SQL Trigger For Trimming String Fields
Jul 20, 2005
greetings!! the below trigger works fine in SQL 2000 and doesn't take
up much resources, it's a very simple solution to trim text fields at
input in SQL. I know a lot of folks will say to create input masks at
the UI level and that SQL is a restricted back-end DB, but to be quite
honest I don't trust those UI guys and as a DBA I will have to clean
the mess when they forget to validate. Hope it can be usefull to any
of you too. ;)
----------------------------------------------------------------------
CREATE TRIGGER [TRIMMER_TGR] ON [dbo].[MyTable]
AFTER INSERT, UPDATE
AS
IF UPDATE (MyStringField)
DECLARE @TRIMMEDFIELD NVARCHAR(50)
DECLARE @MYID INT
SELECT @TRIMMEDFIELD = MyStringField from Inserted
SELECT @MYID = ID from Inserted
BEGIN
UPDATE tblDocket SET MyStringField = RTRIM(LTRIM(@TRIMMEDFIELD))
WHERE ID = @MYID
END
View 6 Replies
ADVERTISEMENT
Nov 29, 2007
I have an SQL 2000 database with several tables that are populated using various forms. One or more of the forms is causing the several fields to fill in with data input plus empty spaces to the right for the full character length assigned. Is there a routine or database feature in Visual Studio that can trim off all trailing spaces in all fields in a database table? Is there a compacting routine that might be called?
Thank you
View 6 Replies
View Related
Jul 14, 2006
I have a trigger that writes changes to an audit table. In the case of Updates, I only want to write out the fields that have actually changed. My code for COLUMNS_UPDATED evaluates to true for all varchar fields, even when they haven't changed. All other scenarios appear to be working correctly.
WHILE @field < @maxfield
BEGIN
SELECT @field = min(ORDINAL_POSITION) FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = @TableName AND ORDINAL_POSITION > @field
SELECT @bit = (@field - 1 )% 8 + 1
SELECT @bit = power(2,@bit - 1)
SELECT @char = ((@field - 1) / 8) + 1
IF ((@TriggerType = 'I') OR (@TriggerType = 'D') OR (@TriggerType = 'U' AND substring (COLUMNS_UPDATED(),@char, 1) & @bit > 0))
BEGIN..........
END
END
Ideas? Thanks!
View 1 Replies
View Related
Jun 7, 2007
Hi,
I want to trim new line from string in sql server 2005...
I prepared one dtsx package and in that I want to trim new line from one of my column but i am unable to do that.
for example:
I want to retrive one column name DESCRIPTION and put that in text file but along with it I am getting blank space and new line also that I want to trim...
T.I.A
View 1 Replies
View Related
Dec 20, 2005
Hi all,
I have a ranking system where I wish to update the ranking every time a result is reported. Performance is no issue what-so-ever. More specifically, two players are to devide their points after each series of games to reflect the fraction of over-all games that each player have won.
I've written the trigger below, but Visual Web Developer 2005 Express (SQL Server 2005 Express) complains about the references to the 'inserted'-table.
I find it very difficult to transform the code below to something that looks like the examples found in documentation.
Could someone get me started in the right direction?
Thanks in advance,
Anders
create trigger result_insert on result
after insert as
begin
declare @won1 as int
declare @won2 as int
declare @oldRank1 as float
declare @oldRank2 as float
declare @oldranksum as float
select @won1 = sum(wongames1) from result where player1 = inserted.player1 and player2=inserted.player2
select @won2 = sum(wongames2) from result where player1 = inserted.player1 and player2=inserted.player2
select @oldrank1 = Rank from RankingInfo where memberid = inserted.playerid1
select @oldrank2 = Rank from RankingInfo where memberid = inserted.playerid2
set @oldranksum = @oldrank1 + @oldrank2
update rankingInfo set Rank = @won1 / ( @won1+@won2) * @oldranksum where memberid = inserted.player1
update rankingInfo set Rank = @won2 / ( @won1+@won2) * @oldranksum where memberid = inserted.player2
end
View 1 Replies
View Related
Jul 23, 2005
Hi,I want to log updates to specific fields, storing the new and oldvalues. Is there any way I can iterate the collection of updatedfields within a trigger in order accomplish this?Thanks in advance,Julie Vazquez
View 4 Replies
View Related
Apr 28, 2006
I have data looks like below
23-4c-4f-3rd-2
23-5c-4ad-3-2
THis is all in one field. I need to use sql to take out the data between the 1st and third hyphens to return
4c-3rd
5c-4ad
View 2 Replies
View Related
Oct 13, 2013
I need creating an update trigger that involved two tables and a few fields.
tblCases
Fields
Defendent1
Defendent2
Defendant3
tblCaseBillingDetails
Fields
DefCount
I would like to create the trigger on tblCaseBillingDetails so that when the data in the Defendant fields are updated, the trigger fires and updates the Defendant count DefCount.
View 1 Replies
View Related
Jan 8, 2008
I hava at large database and I need to search all tables for the string 'NaN' and replace it to null to enable conversion.
Is there any way to do this so that i do not need to write an update query for every column?
Would appretiate an example if anyone knows how to manage this.
Thanks!
View 2 Replies
View Related
Jul 20, 2005
I have a table with a startdatetime and an enddatetime column such as:StartDateTime EndDateTime what I want to see returnedis:01/29/2004 10:30AM 01/29/2004 1:30PM "1/29/2004 10:30AM - 1:30PM"01/29/2004 10:30AM 01/30/2004 1:30PM "1/29/2004 10:30AM - 1/30/20041:30PM"01/29/2004 10:30AM 01/30/2004 10:30AM "1/29/2004 10:30AM - 1/30/200410:30AM"Maybe someone has accomplished this aready in a stored procedure andhas an example of how to do it?lq
View 2 Replies
View Related
May 26, 2006
Hi All,I'm familiar with the syntax that looks like this:SELECT * FROM Users WHERE Email LIKE '%aol%'Which would return all users that whose Email column contains "aol".However, if I wanted to do that sort of a search using a prepared statement, how would I do it?I can't simply doSELECT * FROM Users WHERE Email LIKE '%@MatchAgainst%'Can somebody clue me in?
View 1 Replies
View Related
Apr 23, 2014
I have a field that will look something like this. I need to read a file and when the first 3 positions are REM, the number following is the Order number, and the initials, are the rep. this is how they are tracking for the rep, if they sent reminders to customers and when.
REM 0482348 LV 04/13
I would like to do this in SQL and also join to the order header file that will give me the invoice total for that order. what is the best approach here via SQL for these first 2 fields' needed?
View 1 Replies
View Related
Sep 20, 2007
Hi
I need to export data to CSV file from Sql Reporting Services.
I am including single quotations in my view to display in SRS for string fields.
After export if we open in Notepad, for string field it is adding multiple quotations.
How to add single quotations for string fields - CSV files,
Please let me know if anybody knows.
Thx
Vijji
View 4 Replies
View Related
Feb 6, 2007
I've got a report built and I'm trying to figure out how sorting and grouping works. I can group the report by Patient, Albumin and it groups as I would expect.
Patient Date Albumin
Adams, John 01/28/2007 4.1
Adams, John 12/30/2007 3.9
Adams, John 01/15/2007 3.2
Barker, Mark 01/18/2007 4.3
Barker, Mark 01/22/2007 4.1
Barker, Mark 01/05/2007 3.9
However, when I try to group by Albumin, Patient, it just sorts by Albumin.
Patient Date Albumin
Barker, Mark 01/18/2007 4.3
Adams, John 01/28/2007 4.1
Barker, Mark 01/22/2007 4.1
Adams, John 12/30/2007 3.9
Barker, Mark 01/05/2007 3.9
Adams, John 01/15/2007 3.2
What I'm looking for is this:
Patient Date Albumin
Barker, Mark 01/18/2007 4.3
Barker, Mark 01/22/2007 4.1
Barker, Mark 01/05/2007 3.9
Adams, John 01/28/2007 4.1
Adams, John 12/30/2007 3.9
Adams, John 01/15/2007 3.2
Is this something that can be done with grouping and sorting?
Thanks,
Chad
View 1 Replies
View Related
Apr 21, 2008
hi
i have table i use it for update insert
and the users use this table from a grid on the web
and i need to prevent from white space in the fields in table
so how to
create TRIGGER remove white space from a fields in table scan and fix it ?
Code Snippet
SELECT TRIM(fieldname)
, LTRIM(fieldname)
, RTRIM(fieldname)
, LTRIM(RTRIM(fieldname))
FROM tablename
Code Snippet
WHERE (LTRIM(RTRIM(fieldname)) = 'Approve')
Code Snippet
replace(@text,' ','')
create TRIGGER on update insert and not to damage the text in the all fields
TNX
View 21 Replies
View Related
Mar 31, 2008
Hello all, i'm new to using sql server. i was wondering if i anyone can assist me with the following:
M5 - AJJJJJJJ
How can i just get the '5' in another column? Thank you.
View 20 Replies
View Related
Jun 21, 2006
Hello all,I'm at a loss on how to do this. We're using MS SQL 2000 Server and Ihave a list of fields I need to find the first and last entry for.Here's an example of the table:Number - VarChar(10)Jan - IntFeb - IntMar - IntApr - IntMay - IntJune - IntANd it'll look something like this:NumberJanFebMarAprMayJun12322001901922012032054432433322 4565423754694665And I need to create a table with this:NumberFirstLastDifference123220020554432433456235423754665-89I'm not sure if this'll copy over correctly, but I have gaps in thedata so I can't just say Jun-Jan, but I need tofind the first fieldwith data and last field with data, then find the difference of these.Suggestions? Is there a loop or something I can do in TSQL that'll dothis? I'd like to do this in Query Analyzer since it's just a one-timereport. Thanks --Alex
View 2 Replies
View Related
Aug 17, 2005
I have a field in my SQL table that is defined Decimal(10,8), which
provides me with the ability to have up to 8 digits after the decimal
place, however, if I store a value such as 3.14, it is stored as
3.14000000. This is fine in the database, but it is the same when it's
returned. I have tried using Convert.ToDecimal on my returned row but
it doesn't work. The value at runtime appears as 3.14D in the locals
window but displays with all the insignifcant digits.
Any ideas what's happening or if there is an easy way to trim the trailing zeros?
View 2 Replies
View Related
Mar 16, 2004
Hi,
Probably easy but I'd like the best way to do it:
I have a datetime column with A LOT of rows. I'd like a select statement to return each date with everything after the hour part set to zero.
For example, if my column has the following values:
2004-11-03 13:03:36.876
1999-02-17 01:56:12.126
Should return:
2004-11-03 13:00:00.000
1999-02-17 01:00:00.000
I know I can do this with a function and a couple of casts but there must be an easier (and faster) way!?!?
Thanks,
Skip.
View 1 Replies
View Related
Mar 9, 2006
I need to trim what looks like two spaces from a field.
Example:
" 601274" needs to be "601274". Does anyone know the syntax?+
View 9 Replies
View Related
Jul 20, 2005
HiI have a DTS package that pulls data from oracle and inserts it into SQL.During this transfer, any data that has trailing spaces, loses those spacesin SQL. i.e. it's been trimmed.Any way to set DTS not to trim the data ?ThanksSteve
View 1 Replies
View Related
Jul 19, 2006
Hi
I have a question regarding the trimming of some numbers at the end of a column
I have a resource table containing the name column for Ex : Santhosh, Kumar Edukulla X21646. Is there any way we can find the way to remove the numbers at the end from this column and copy to a new column in SQL SERVER.
Thanks in Advance!
Santhosh
View 1 Replies
View Related
Apr 14, 2006
I have a field called sku in a table... many of the sku fields have data as ep12345678. I need a sql statement that will update all data in that field from ep12345678 to 12345678.
So I need to trim off the 'ep' Any one know how to do that?
View 8 Replies
View Related
Feb 23, 2015
I have a SQL select syntax as below
0 AS SalaryMin,
2088 AS SalaryMax,
2088 AS BillableHours,
'Month' AS SalaryPaidCode,
0 AS SalaryBreakdownHourly,
0 AS SalaryBreakdownDaily,
[Code] ...
While outputting to CSV.file
I got :0,2088,2088,"Month",0,0,0,0,0,0,0,"N/A","N/A","G","N/A","Exempt","Other",1
How can I remove all double quotes in the string fields? so that O can get the result as below while the output
0,2088,2088,Month,0,0,0,0,0,0,0,N/A,N/A,G,N/A,Exempt,Other,1
View 7 Replies
View Related
May 31, 2007
mssql 2000, asp.net(vbscript)
How am i able to trim leading zeros? Right now i have two values:00000005 500000010 1000000015 15..... etc...
how do i write a query where i can select an argument where 5 = 0000005?
the column with 00000005 is varchar and5 is numeric
View 4 Replies
View Related
Apr 10, 2008
Hi all,
i have a table in SQL with +- 5000 phone numbers in various styles
e.g.
0515 - 578989
033 2981861
+31 (0)30 602 44 44
03.28.68.63.18
03/658.85.80
I want this to convert to international format like this
003237778899
0031306024444
Do i have to write 10 replace after each other or is there an easier way to format the phone numbers?
Thx in advance!!
View 8 Replies
View Related
Mar 9, 2006
I would love to see an automatic trimming transformation. We currently have 14 different ERP systems throughout our company and almost everything we do, when it comes to strings, requires trimming. Although doing it through the derived column task is somewhat easier, it is very time consuming and error proned, if you overlook a column or two.
-BradG
View 2 Replies
View Related
Jan 19, 2007
Hi,
I am adding some empty spaces to the string value but it comes out trimmed.
How can I fix that?
Thanks,
Igor
View 2 Replies
View Related
Jan 7, 2008
Hi
Is there any way to trim all the leading and trailing spaces in all the column in a table.
JigJan
View 5 Replies
View Related
Jul 13, 2007
Im trying to determine the best way to store data gathered from a form that a user will fill out online. The form is dynamic and is customized at run time based on group-specific criteria. The end result is a form that might have 3 extra text boxes, 2 extra sets of radio buttons and a freeform textbox, whereas for another group, there might be a slightly different set of input fields. Now comes the issue of storing this data. Since the fields can be somewhat dynamic, it could get tricky to define table columns for each possible input field. So Im considering storing the data as xml. Has anyone else had to build custom forms and ended up storing the data as xml ?
View 2 Replies
View Related
Mar 22, 2006
I have two tables:tb_news---story_id-productlisttb_lookup---story_id-productwhen an insert command is run on tb_news, productlist field ispopulated with a value such as 'abc, def, de'when this happens, i need tb_lookup to be populated with seperaterecords for each product in productlist and the story_id from tb_news.Example:INSERT INTO tb_news (story_id, product_list)VALUES (12345, 'abc, def, de')Results:tb_news--12345, 'abc, def, de'tb_lookup--12345, 'abc'12345, 'def'12345, 'de'Ideally, I would like this to use recursion and give me tha ability tochange the delimiter at any time (might not always be a comma). someproducts may have a period in them. number of products is unknown andmight be 0 (field may be empty or NULL).
View 2 Replies
View Related
Feb 20, 2004
Dear everyone,
I would like to create auto-generated "string" ID for any new record inserted in SQL Server 2000.
I have found some SQL Server 2000 book. But it does not cover how to create procedure or trigger to generate auto ID in the string format.
Could anyone know how to do that?? Thanks!!
From,
Roy
View 7 Replies
View Related
Feb 23, 2006
I have a Table Name "Forums". I want to ceate an AFTER-Trigger on it. It will execute when ever a new row is inserted to "Froums" Table.
Here is what I did but It needs to be corrected:
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
ALTER TRIGGER CreateTopicsTableTrigger
ON dbo.Forums
AFTER INSERT
AS
SET NOCOUNT OFF
DECLARE @myNewForum varchar
CAST(@@ROWCOUNT as varchar) /*Is it OK???*/
SET @myNewForum=@myNewForum+@@ROWCOUNT /*Here I dont know how assigments work in SQL*/
GO
CREATE Table @myNewForum /*Will this work some how???*/
( TopicID int IDENTITY NOT NULL, TopicTitle varchar(50) , CreatedBy varchar(50) ,
DateCreated DateTime , DateLastUpdate DateTime , LastUpdateBy varchar(50) )
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
View 5 Replies
View Related