some_bloody_fool
some_bloody_fool

Reputation: 4685

How to remove anything that is not a number from a raw string input in SQL Server 2005 or 2008

Is there an efficient way using sql server 2005 to remove all characters in a string like this which are not numbers?

TEXT T EXT TEXT 2345 TEXT SDTE

I was thinking there might be a way to combine the replace statement with a regular expression.

Upvotes: 1

Views: 332

Answers (1)

Michael Fredrickson
Michael Fredrickson

Reputation: 37398

Here is the answer from the related question, with the slight change needed to remove non-numbers as opposed to non-alphas:

Create Function [dbo].[RemoveNonNumericCharacters](@Temp VarChar(1000))
Returns VarChar(1000)
AS
Begin

    While PatIndex('%[^0-9]%', @Temp) > 0
        Set @Temp = Stuff(@Temp, PatIndex('%[^0-9]%', @Temp), 1, '')

    Return @Temp
End

Upvotes: 1

Related Questions