473,387 Members | 1,569 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

SQL Trigger for trimming String fields

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
Jul 20 '05 #1
6 11422
On 23 Apr 2004 12:12:18 -0700, Juniorlinn wrote:
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. ;)


Unfortunately, you didn't test it very well. For starters, you never
tested it in a database with the recursive triggers option set to on.
And you didn't test the version you posted here, since you specify it
as a trigger on MyTable, yet you perform an UPDATE of tblDocket.
You didn't try this either:

UPDATE MyTable
SET MyStringField = ' ab '
WHERE (anything that matches 2 rows or more)

(or -equivalent- an insert ... select with 2 or more rows)
And you also didn't test:

UPDATE MyTable
SET AnotherColumn = 123
WHERE (anything that matches at least 1 row)

Here's a better way to achieve what you want:

CREATE TRIGGER [TRIMMER_TGR] ON [dbo].[MyTable]
AFTER INSERT, UPDATE
AS
IF UPDATE (MyStringField)
BEGIN
UPDATE tblDocket
SET MyStringField = RTRIM(LTRIM(MyStringField))
WHERE ID IN (SELECT ID FROM inserted)
END
Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Jul 20 '05 #2
>> 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. <<

The people that feel that way are called "experts"; but you are not
paranoid about the current crop of app developers. We seem to have a
crop of people who can pass a certification exam, but not solve a
problem.

Instead of a trigger, which is procedural, proprietary and expensive,
why not use a constraint? That is declarative, portable and is fired
only on the row(s) involved instead of the whole table?

CREATE TABLE Floob
(...
foobar VARCHAR(15) DEFAULT '{{missing}}' NOT NULL
CHECK (foobar = RTRIM(LTRIM(foobar))),
..);

This will make the UI people do something on their tier because the
database will kick it back to them.
Jul 20 '05 #3
You can change the constraint to:
CHECK (DATALENGTH(foobar) = DATALENGTH(RTRIM(LTRIM(foobar))))

HTH,
Gert-Jan

Juniorlinn CC wrote:

Hi CELKO - The table was already in place with many many rows, so I
created that exact Constraint you exposed here using the 'Manage
Constraints' graphical tool in EM, but it didn't work. If UI sends blank
spaces after the string (coming from web-forms), they will not be
filtered off by this constraint. Any suggestions?

txn
Jrlin

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!


--
(Please reply only to the newsgroup)
Jul 20 '05 #4
Very well noticed Hugo, blessings for pointing missing parts in the
strip of code I've shared here and exposing a more elegant way of
writing it, sorry for switching the table names at posting time, I had
to modify it a bit for simplicity before posting and I had a little
accident renaming the tables, promise to pay more attention next time.
Although I was aware of not being able to use recursive triggers in this
scenario, this code was appropriated for the particular problem I needed
to address and it worked very well for single row updates and inserts,
otherwise I would use Cursors. Thanks again for sharing your marvels,
much appreciated.

;)
Jrlin

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #5
Hi CELKO - The table was already in place with many many rows, so I
created that exact Constraint you exposed here using the 'Manage
Constraints' graphical tool in EM, but it didn't work. If UI sends blank
spaces after the string (coming from web-forms), they will not be
filtered off by this constraint. Any suggestions?

txn
Jrlin

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #6
You can change the constraint to:
CHECK (DATALENGTH(foobar) = DATALENGTH(RTRIM(LTRIM(foobar))))

HTH,
Gert-Jan

Juniorlinn CC wrote:

Hi CELKO - The table was already in place with many many rows, so I
created that exact Constraint you exposed here using the 'Manage
Constraints' graphical tool in EM, but it didn't work. If UI sends blank
spaces after the string (coming from web-forms), they will not be
filtered off by this constraint. Any suggestions?

txn
Jrlin

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!


--
(Please reply only to the newsgroup)
Jul 20 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Trevor Fairchild | last post by:
I am trying to create a very minimal auditing system for a series of databases. I am in the process of writing Update triggers for 5 Tablse. I will write a trigger for each table-the trigger's...
4
by: Juniorlinn | last post by:
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...
9
by: Martin | last post by:
Hello, I'm new with triggers and I can not find any good example on how to do the following: I have two tables WO and PM with the following fields: WO.WONUM, VARCHAR(10) WO.PMNUM,...
3
by: takilroy | last post by:
Hi, Does anyone know of a simple way to do this? I want to create an insert trigger for a table and if the record already exists based on some criteria, I want to update the table with the...
4
by: Jules Alberts | last post by:
Hello everyone, I'm working on a tiny trigger function that needs to ensure that all values entered in a field are lowercase'd. I can't use pl/pgsql because I have a dozen different columns...
16
by: Josué Maldonado | last post by:
Hello list, The TCL trigger that uses NEW and OLD arrays failed after after I removed a unused column, now I got this error: pltcl: Cache lookup for attribute '........pg.dropped.24........'...
0
by: JohnO | last post by:
Thanks to Serge and MarkB for recent tips and suggestions. Ive rolled together a few stored procedures to assist with creating audit triggers automagically. Hope someone finds this as useful as...
6
by: Oliver | last post by:
I'm fairly new to DB2. I have been assigned to build a delete trigger that finds the data type of each of the table's fields so that the trigger can then build a string consisting of OLD values...
3
by: lenygold via DBMonster.com | last post by:
Thank you very much SERGE for your help. I found example in Graeme Birchall COOKBOOK wich i think exactly what i need for SQL check in triggers: • User query joins to table function - sends...
2
by: somacore | last post by:
Hello all, I am trying to implement a trigger on a Microsoft SQL Server 2000 database. I have created the trigger, and it works mostly like it should, however I have a slight problem I can't seem...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.