String cleaning with TSQL - SimonS Blog on SQL Server Stuff

String cleaning with TSQL

There have been some posts on cleaning text with TSQL, Remove all non-numeric characters from a string and extract-only-numbers-from-a-string

Here is my take on it. The nice thing is that you just specify the valid characters in the like pattern to select those that you want.return.

declare @string varchar(200)

set @string = 'this $%^^&is%^& s2342om23&&({}e c76l232e+_+a#n/ c][#o''d#e'

 

select cast(cast((select substring(@string,n,1)

from num

where n <= len(@string)

and substring(@string,n,1) like '[a-z ]' for xml path('')) as xml)as varchar(max))

 



-
Published 26 February 2008 13:35 by simonsabin
Filed under:

Comments

26 February 2008 15:04 by Alex_Kuznetsov

# re: String cleaning with TSQL

Great! I love it!

27 February 2008 08:50 by Madhivanan

# re: String cleaning with TSQL

Good one Simon Smile