Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Trimming special characters


Trimming special characters

Author
Message
GoldenDBA
GoldenDBA
SSC-Enthusiastic
SSC-Enthusiastic (197 reputation)SSC-Enthusiastic (197 reputation)SSC-Enthusiastic (197 reputation)SSC-Enthusiastic (197 reputation)SSC-Enthusiastic (197 reputation)SSC-Enthusiastic (197 reputation)SSC-Enthusiastic (197 reputation)SSC-Enthusiastic (197 reputation)

Group: General Forum Members
Points: 197 Visits: 814
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
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45215 Visits: 39926
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 Row-By-Agonizing-Row.
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.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
GoldenDBA
GoldenDBA
SSC-Enthusiastic
SSC-Enthusiastic (197 reputation)SSC-Enthusiastic (197 reputation)SSC-Enthusiastic (197 reputation)SSC-Enthusiastic (197 reputation)SSC-Enthusiastic (197 reputation)SSC-Enthusiastic (197 reputation)SSC-Enthusiastic (197 reputation)SSC-Enthusiastic (197 reputation)

Group: General Forum Members
Points: 197 Visits: 814
Thanks Jeff...that makes a lot of sense! :-D
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45215 Visits: 39926
SQLJocky (12/20/2012)
Thanks Jeff...that makes a lot of sense! :-D


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 Row-By-Agonizing-Row.
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.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
GoldenDBA
GoldenDBA
SSC-Enthusiastic
SSC-Enthusiastic (197 reputation)SSC-Enthusiastic (197 reputation)SSC-Enthusiastic (197 reputation)SSC-Enthusiastic (197 reputation)SSC-Enthusiastic (197 reputation)SSC-Enthusiastic (197 reputation)SSC-Enthusiastic (197 reputation)SSC-Enthusiastic (197 reputation)

Group: General Forum Members
Points: 197 Visits: 814
Yes. I passed on the recommendation with an example to the developer and told him to test it out. :-)
dwain.c
dwain.c
SSCarpal Tunnel
SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)

Group: General Forum Members
Points: 4275 Visits: 6431
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.


My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
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?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search