Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Trimming special characters Expand / Collapse
Author
Message
Posted Thursday, December 20, 2012 10:11 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Tuesday, August 26, 2014 8:04 PM
Points: 158, Visits: 622
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
Posted Thursday, December 20, 2012 11:38 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 9:58 AM
Points: 36,995, Visits: 31,517
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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1399082
Posted Thursday, December 20, 2012 2:57 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Tuesday, August 26, 2014 8:04 PM
Points: 158, Visits: 622
Thanks Jeff...that makes a lot of sense!
Post #1399116
Posted Thursday, December 20, 2012 6:09 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 9:58 AM
Points: 36,995, Visits: 31,517
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 "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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1399147
Posted Friday, December 21, 2012 5:18 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Tuesday, August 26, 2014 8:04 PM
Points: 158, Visits: 622
Yes. I passed on the recommendation with an example to the developer and told him to test it out.
Post #1399635
Posted Monday, December 24, 2012 7:08 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 8:30 PM
Points: 3,631, Visits: 5,281
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!
Post #1400015
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse