Labels

Wednesday, July 24, 2013

SQL UDF to get all integers from Text


CREATE FUNCTION [dbo].[fn_GetIntFromText] (@name VARCHAR(max))
RETURNS NVARCHAR(max)
AS
BEGIN
       DECLARE @str VARCHAR(MAx)

       SET @str = @name

       WHILE PATINDEX('%[^0-9]%', @str) > 0
              SET @str = REPLACE(@str, SUBSTRING(@str, PATINDEX('%[^0-9]%', @str), 1), '')

       RETURN replace(@str, '-', '')
END

1 comment: