Tuesday, July 28, 2015

How to separate out numbers from a string in SQL Server

At a time we will have a requirement to seperate out numbers from a string.

Use below query to seperate out numbers from the string:

DECLARE @strValue VARCHAR(256) = 'asdf1234.a1s2d3f4@@@'
DECLARE @IntPos INT
SET @IntPos = PATINDEX('%[^0-9]%', @strValue)
WHILE @IntPos > 0
BEGIN
SET @strValue = STUFF(@strValue, @IntPos, 1, '' )
SET @IntPos = PATINDEX('%[^0-9]%', @strValue )
END
SELECT @strValue


Labels: , ,

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home