how to strip non alpha numeric characters in sql query

  • Hello all,

    I need to create a udf that will strip non alpha numeric characters (including spaces) from a given string.  Any idea how this can be done?

    Regards,

    Joshua

  • CREATE  FUNCTION dbo.test (@inputstring varchar(255))

    RETURNS varchar(255)

    AS

    BEGIN

    DECLARE @outputstring varchar(255)

    SET @outputstring = @inputstring

    DECLARE @pos int

    SET @pos = PATINDEX('%[^0-9A-Z]%',@outputstring)

    WHILE (@pos > 0)

    BEGIN

    SET @outputstring = STUFF(@outputstring,@pos,1,'')

    SET @pos = PATINDEX('%[^0-9A-Z]%',@outputstring)

    END

    RETURN @outputstring

    END

    Far away is close at hand in the images of elsewhere.
    Anon.

  • Just to add a little more to David Solution. Be careful with what you consider ALPHANUMERIC because some systems may need to use signs other characters and All you have to do is to include those on the PATINDEX pattern ex. '%[^0-9A-Z.-+]%'

    So, Know your DATA First!

     

    Just My $0.02

     


    * Noel

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply