Thursday, February 12, 2015

Get Integer from a String using MS SQL Server

Execute the following:

DECLARE @strAlphaNumeric VARCHAR(256)

SET @strAlphaNumeric = 'Hello2 World'

DECLARE @intAlpha INT

SET @intAlpha = PATINDEX('%[^0-9]%', @strAlphaNumeric)



BEGIN
 
WHILE @intAlpha > 0



BEGIN
 
SET @strAlphaNumeric = STUFF(@strAlphaNumeric, @intAlpha, 1, '' )

SET @intAlpha = PATINDEX('%[^0-9]%', @strAlphaNumeric )



END

END
 
SELECT ISNULL(@strAlphaNumeric,0)
 
 


OUTPUT:
2

No comments:

Post a Comment