Skip to content
 

Replace Invalid Characters

Unfortunately I haven’t found a way to remove invalid characters without looping. If anyone knows a way please post.

This script will remove invalid characters:

--Leave only numbers
WHILE PATINDEX('%[^0-9]%',@string) > 0
 BEGIN

    SET @pos = PATINDEX('%[^0-9]%',@string)
    SET @string = REPLACE(@string,SUBSTRING(@string,@pos,1),'')

 END

--Leave only characters
WHILE PATINDEX('%[^a-z]%',@string) > 0
 BEGIN

    SET @pos = PATINDEX('%[^0-9]%',@string)
    SET @string = REPLACE(@string,SUBSTRING(@string,@pos,1),'')

 END
Share and Enjoy:
  • Print this article!
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks

Related Posts:

  • » SQL Server Add Primary Key
  • » SQL Server Select
  • » Compare Stored Procedure Dataset Output
  • » Get End of Month
  • » Beginning of Month
  • blog comments powered by Disqus