How to strip all non-alphabetic characters from string in SQL Server?

How could you remove all characters that are not alphabetic from a string?

What about non-alphanumeric?

Does this have to be a custom function or are there also more generalizable solutions?


  • Try this function:

    Create Function [dbo].[RemoveNonAlphaCharacters](@Temp VarChar(1000))
    Returns VarChar(1000)
        Declare @KeepValues as varchar(50)
        Set @KeepValues = '%[^a-z]%'
        While PatIndex(@KeepValues, @Temp) > 0
            Set @Temp = Stuff(@Temp, PatIndex(@KeepValues, @Temp), 1, '')
        Return @Temp

    Call it like this:

    Select dbo.RemoveNonAlphaCharacters('abc1234def5678ghi90jkl')

    Once you understand the code, you should see that it is relatively simple to change it to remove other characters, too. You could even make this dynamic enough to pass in your search pattern.