Log in
::
Register
::
Not logged in
Home
Tags
Articles
Editorials
Stairways
Forums
Scripts
Videos
Blogs
QotD
Books
Ask SSC
SQL Jobs
Training
Authors
About us
Contact us
Newsletters
Write for us
Recent Posts
Recent Posts
Popular Topics
Popular Topics
Home
Search
Members
Calendar
Who's On
Home
»
SQL Server 7,2000
»
T-SQL
»
Trimming special characters
Trimming special characters
Rate Topic
Display Mode
Topic Options
Author
Message
SQLJocky
SQLJocky
Posted Thursday, December 20, 2012 10:11 AM
SSC-Enthusiastic
Group: General Forum Members
Last Login: Saturday, May 18, 2013 10:42 PM
Points: 146,
Visits: 545
One of our developers created this UDF to trim out special characters...is there a better way to do this?
ALTER FUNCTION [dbo].[UDF_TrimSpecialCharacter]
(
-- Add the parameters for the function here
@String varchar(100)
)
RETURNS VARCHAR(100)
AS
BEGIN
RETURN replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(@String,'-',''),',',''),'_',''),' ',''),'*',''),'.',''),'/',''),'\',''),'(',''),')',''),'#',''),':',''),';',''),'@',''),'~',''),'&','')
END
Post #1399044
Jeff Moden
Jeff Moden
Posted Thursday, December 20, 2012 11:38 AM
SSC-Dedicated
Group: General Forum Members
Last Login: Yesterday @ 2:32 PM
Points: 32,906,
Visits: 26,792
SQLJocky (12/20/2012)
One of our developers created this UDF to trim out special characters...is there a better way to do this?
ALTER FUNCTION [dbo].[UDF_TrimSpecialCharacter]
(
-- Add the parameters for the function here
@String varchar(100)
)
RETURNS VARCHAR(100)
AS
BEGIN
RETURN replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(@String,'-',''),',',''),'_',''),' ',''),'*',''),'.',''),'/',''),'\',''),'(',''),')',''),'#',''),':',''),';',''),'@',''),'~',''),'&','')
END
Only slightly. The nested replaces are incredibly fast and, short of a CLR function, is probably the fastest method. The only other speed enhancement I can see is that it should be converted to an inline table valued function even though it returns a scalar value. To be sure, converting to an inline table valued function that does this can increase the speed of the function by 2 to 7 times. Please see the following article on that...
http://www.sqlservercentral.com/articles/T-SQL/91724/
From a functionality standpoint, I'd make @String a VARCHAR(8000) instead of VARCHAR(100).
--Jeff Moden
"
RBAR
is pronounced "ree-bar" and is a "Modenism" for "
R
ow-
B
y-
A
gonizing-
R
ow".
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."
For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
Post #1399082
SQLJocky
SQLJocky
Posted Thursday, December 20, 2012 2:57 PM
SSC-Enthusiastic
Group: General Forum Members
Last Login: Saturday, May 18, 2013 10:42 PM
Points: 146,
Visits: 545
Thanks Jeff...that makes a lot of sense!
Post #1399116
Jeff Moden
Jeff Moden
Posted Thursday, December 20, 2012 6:09 PM
SSC-Dedicated
Group: General Forum Members
Last Login: Yesterday @ 2:32 PM
Points: 32,906,
Visits: 26,792
SQLJocky (12/20/2012)
Thanks Jeff...that makes a lot of sense!
Thanks for the feedback. Don't just take my word for it, though. Test it. Make sure.
--Jeff Moden
"
RBAR
is pronounced "ree-bar" and is a "Modenism" for "
R
ow-
B
y-
A
gonizing-
R
ow".
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."
For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
Post #1399147
SQLJocky
SQLJocky
Posted Friday, December 21, 2012 5:18 PM
SSC-Enthusiastic
Group: General Forum Members
Last Login: Saturday, May 18, 2013 10:42 PM
Points: 146,
Visits: 545
Yes. I passed on the recommendation with an example to the developer and told him to test it out.
Post #1399635
dwain.c
dwain.c
Posted Monday, December 24, 2012 7:08 PM
SSCrazy
Group: General Forum Members
Last Login: Today @ 12:39 AM
Points: 2,345,
Visits: 3,189
Jeff Moden (12/20/2012)
SQLJocky (12/20/2012)
One of our developers created this UDF to trim out special characters...is there a better way to do this?
ALTER FUNCTION [dbo].[UDF_TrimSpecialCharacter]
(
-- Add the parameters for the function here
@String varchar(100)
)
RETURNS VARCHAR(100)
AS
BEGIN
RETURN replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(@String,'-',''),',',''),'_',''),' ',''),'*',''),'.',''),'/',''),'\',''),'(',''),')',''),'#',''),':',''),';',''),'@',''),'~',''),'&','')
END
Only slightly. The nested replaces are incredibly fast and, short of a CLR function, is probably the fastest method. The only other speed enhancement I can see is that it should be converted to an inline table valued function even though it returns a scalar value. To be sure, converting to an inline table valued function that does this can increase the speed of the function by 2 to 7 times. Please see the following article on that...
http://www.sqlservercentral.com/articles/T-SQL/91724/
From a functionality standpoint, I'd make @String a VARCHAR(8000) instead of VARCHAR(100).
Using a binary collation might help also.
No loops! No CURSORs! No RBAR! Hoo-uh!
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Are you too recursively challenged?
Splitting strings based on patterns can be fast!
Post #1400015
« Prev Topic
|
Next Topic »
Permissions
You
cannot
post new topics.
You
cannot
post topic replies.
You
cannot
post new polls.
You
cannot
post replies to polls.
You
cannot
edit your own topics.
You
cannot
delete your own topics.
You
cannot
edit other topics.
You
cannot
delete other topics.
You
cannot
edit your own posts.
You
cannot
edit other posts.
You
cannot
delete your own posts.
You
cannot
delete other posts.
You
cannot
post events.
You
cannot
edit your own events.
You
cannot
edit other events.
You
cannot
delete your own events.
You
cannot
delete other events.
You
cannot
send private messages.
You
cannot
send emails.
You
may
read topics.
You
cannot
rate topics.
You
cannot
vote within polls.
You
cannot
upload attachments.
You
may
download attachments.
You
cannot
post HTML code.
You
cannot
edit HTML code.
You
cannot
post IFCode.
You
cannot
post JavaScript.
You
cannot
post EmotIcons.
You
cannot
post or upload images.
Copyright © 2002-2013 Simple Talk Publishing. All Rights Reserved.
Privacy Policy.
Terms of Use.
Report Abuse.