SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


To remove the specified Characters in the Given String


To remove the specified Characters in the Given String

Author
Message
Ramji Sridharan
Ramji Sridharan
Valued Member
Valued Member (51 reputation)Valued Member (51 reputation)Valued Member (51 reputation)Valued Member (51 reputation)Valued Member (51 reputation)Valued Member (51 reputation)Valued Member (51 reputation)Valued Member (51 reputation)

Group: General Forum Members
Points: 51 Visits: 26
Comments posted to this topic are about the item To remove the specified Characters in the Given String
Kricky
Kricky
SSC Journeyman
SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)

Group: General Forum Members
Points: 82 Visits: 96
Did I miss something here? It looks like the Function is called "dbo.GEN_FN_StripCharacters", but the selects referenced in the comments are "dbo.fn_StripCharacters".
Ed.Wallander
Ed.Wallander
Mr or Mrs. 500
Mr or Mrs. 500 (567 reputation)Mr or Mrs. 500 (567 reputation)Mr or Mrs. 500 (567 reputation)Mr or Mrs. 500 (567 reputation)Mr or Mrs. 500 (567 reputation)Mr or Mrs. 500 (567 reputation)Mr or Mrs. 500 (567 reputation)Mr or Mrs. 500 (567 reputation)

Group: General Forum Members
Points: 567 Visits: 107
Just looks like Ram called the function by an earlier name.

I made a slight modification, converting the string types that will be stripped out to a parameter rather than inputting the expression. I also added more characters to the test string just to be sure it was capturing a few more non-alphanumeric characters for my own peace of mind.


CREATE FUNCTION dbo.GEN_FN_StripCharacters
(
@strMatchType NVARCHAR(3),
@strInputString NVARCHAR(MAX)
)

/*
---Created By : Ram
--Date : 15-Feb-2013
--- Purpose : To remove the specified Characters in the Given String
Alphabetic only: SELECT dbo.fn_StripCharacters('a1!s2@d3#f4$', '^a-z')
Numeric only: SELECT dbo.fn_StripCharacters('a1!s2@d3#f4$', '^0-9+-/')
Alphanumeric only: SELECT dbo.fn_StripCharacters('a1!s2@d3#f4$', '^a-z0-9')
Non-alphanumeric: SELECT dbo.fn_StripCharacters('a1!s2@d3#f4$', 'a-z0-9')

--Modified by : Ed
--Date : 01-Jul-2013
Converted match strings into a match type parameter
A-Alpha
N-Numeric
AN-AlphaNumeric

Precede with ^ to denote NOT

'^AN' = Not AlphaNumeric

*/


RETURNS NVARCHAR(MAX)
AS
BEGIN

DECLARE @strMatchExpression varchar(15)

SET @strMatchExpression = '%[' + CASE @strMatchType
WHEN 'A' THEN 'a-z'
WHEN '^A' THEN '^a-z'
WHEN 'N' THEN '0-9+-/'
WHEN '^N' THEN '^0-9+-/'
WHEN 'AN' THEN 'a-z0-9'
WHEN '^AN' THEN '^a-z0-9'
END + ']%';

WHILE
PATINDEX( @strMatchExpression , @strInputString ) > 0
SET @strInputString = STUFF( @strInputString , PATINDEX( @strMatchExpression , @strInputString ) , 1 , '' );

RETURN @strInputString;
END;

SELECT dbo.GEN_FN_StripCharacters('a','a"1!S2@{]d3#f4$`~''')
SELECT dbo.GEN_FN_StripCharacters('^a','a"1!S2@{]d3#f4$`~''')
SELECT dbo.GEN_FN_StripCharacters('n','a"1!S2@{]d3#f4$`~''')
SELECT dbo.GEN_FN_StripCharacters('^n','a"1!S2@{]d3#f4$`~''')
SELECT dbo.GEN_FN_StripCharacters('an','a"1!S2@{]d3#f4$`~''')
SELECT dbo.GEN_FN_StripCharacters('^an','a"1!S2@{]d3#f4$`~''')



tskelley
tskelley
SSC Eights!
SSC Eights! (806 reputation)SSC Eights! (806 reputation)SSC Eights! (806 reputation)SSC Eights! (806 reputation)SSC Eights! (806 reputation)SSC Eights! (806 reputation)SSC Eights! (806 reputation)SSC Eights! (806 reputation)

Group: General Forum Members
Points: 806 Visits: 1215
Great function! Definitely going into my Hall of Fame!

One other trick it handled nicely was including a decimal in the numeric sort order. For example, I needed 1.11.338 to show before 10.1.002 By adding a decimal at the end of the numeric check, I was able to sort the numbers with multiple decimals correctly.

ufn_StripCharacters([ColumnName], '^0-9+-/.')


Awesome!!!!!!!



Iwas Bornready
Iwas Bornready
SSC-Dedicated
SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)

Group: General Forum Members
Points: 36642 Visits: 886
Thanks for the script.
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