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


Trim Non-Alpha characters from string


Trim Non-Alpha characters from string

Author
Message
Atif-ullah Sheikh
Atif-ullah Sheikh
Hall of Fame
Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)

Group: General Forum Members
Points: 3359 Visits: 5174
Comments posted to this topic are about the item Trim Non-Alpha characters from string

----------------------------------------------------------------------------------------------------------------------------------------------------------------------

Sometimes, winning is not an issue but trying.

You can check my BLOG here


Tom Garth
Tom Garth
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1053 Visits: 1499
Pretty cool.

Tom Garth
Vertical Solutions

"There are three kinds of men. The one that learns by reading. The few who learn by observation. The rest of them have to pee on the electric fence for themselves." -- Will Rogers

katedgrt
katedgrt
SSC Veteran
SSC Veteran (227 reputation)SSC Veteran (227 reputation)SSC Veteran (227 reputation)SSC Veteran (227 reputation)SSC Veteran (227 reputation)SSC Veteran (227 reputation)SSC Veteran (227 reputation)SSC Veteran (227 reputation)

Group: General Forum Members
Points: 227 Visits: 990
Very nice. Just so happens I was bemoaning having to write a 'strip non-numeric characters' function today. You probably saved me an hour! BigGrin

Cool Kate The Great w00t
If you don't have time to do it right the first time, where will you find time to do it again?
katedgrt
katedgrt
SSC Veteran
SSC Veteran (227 reputation)SSC Veteran (227 reputation)SSC Veteran (227 reputation)SSC Veteran (227 reputation)SSC Veteran (227 reputation)SSC Veteran (227 reputation)SSC Veteran (227 reputation)SSC Veteran (227 reputation)

Group: General Forum Members
Points: 227 Visits: 990
I did get an error applying to a nullable column. (LEN=0; Error was "TOP clause contains an invalid value.") Because I'm in a hurry I just threw a case statement around the function call but a clause in the function to work around that would be a nice enhancement.

Cool Kate The Great w00t
If you don't have time to do it right the first time, where will you find time to do it again?
James Zimmerman
James Zimmerman
SSC Rookie
SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)

Group: General Forum Members
Points: 29 Visits: 302
Thank you for this example.
If you do much data cleansing or string manipulation, keep in mind that you can implement regular expression functionality in SQL Server 2005 and above. It's easier than I thought it would be, assuming your DBA allows you to enable CLR integration.
This article lays it out very well:
http://justgeeks.blogspot.com/2008/08/adding-regular-expressions-regex-to-sql.html
Adam Sottosanti
Adam Sottosanti
SSC-Enthusiastic
SSC-Enthusiastic (150 reputation)SSC-Enthusiastic (150 reputation)SSC-Enthusiastic (150 reputation)SSC-Enthusiastic (150 reputation)SSC-Enthusiastic (150 reputation)SSC-Enthusiastic (150 reputation)SSC-Enthusiastic (150 reputation)SSC-Enthusiastic (150 reputation)

Group: General Forum Members
Points: 150 Visits: 404
Thanks for the script, I like they way you created the tally table on the fly lilke that, I never thought of doing that! Here is what I've been using to do the same thing, it's pretty easy to modify to include/exclude numerics, spaces, certain special characters etc... which is why I like it.



DECLARE @data VARCHAR(100)
SET @data = '!2131231Atif123123 234234Sheikh6546'
WHILE PATINDEX('%[^a-z ]%', @data) > 0
SET @data = STUFF(@data, PATINDEX('%[^a-z ]%', @data), 1, '')
SELECT
RTRIM(LTRIM(@data)) AS Data




Thanks again!

Adam Sottosanti
frederico_fonseca
frederico_fonseca
SSC-Enthusiastic
SSC-Enthusiastic (174 reputation)SSC-Enthusiastic (174 reputation)SSC-Enthusiastic (174 reputation)SSC-Enthusiastic (174 reputation)SSC-Enthusiastic (174 reputation)SSC-Enthusiastic (174 reputation)SSC-Enthusiastic (174 reputation)SSC-Enthusiastic (174 reputation)

Group: General Forum Members
Points: 174 Visits: 1091
works only partially - does not take in consideration that fact that some countries do have Accented alphas so the code as is removes them.

Select dbo.fnTrimNonAlphaCharacters('âáÉéôô') will just return an empty string for example.

one possible change to the code to allow for the above is

WHERE N <= LEN(@pString)
And (Ascii(SUBSTRING(@pString collate SQL_Latin1_General_CP1253_CI_AI,N,1)) between 97 and 122
Or Ascii(SUBSTRING(@pString collate SQL_Latin1_General_CP1253_CI_AI,N,1)) between 65 and 90
Or Ascii(SUBSTRING(@pString collate SQL_Latin1_General_CP1253_CI_AI,N,1)) = 32)
Iwas Bornready
Iwas Bornready
SSCrazy Eights
SSCrazy Eights (9.8K reputation)SSCrazy Eights (9.8K reputation)SSCrazy Eights (9.8K reputation)SSCrazy Eights (9.8K reputation)SSCrazy Eights (9.8K reputation)SSCrazy Eights (9.8K reputation)SSCrazy Eights (9.8K reputation)SSCrazy Eights (9.8K reputation)

Group: General Forum Members
Points: 9768 Visits: 885
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