T-SQL.co.uk
0 votes



CREATE FUNCTION dbo.fn_KeepOnlyNumbers (@input NVARCHAR(MAX))

RETURNS NVARCHAR(MAX)

AS

BEGIN

    WHILE PATINDEX('%[^0-9]%', @input) > 0

    BEGIN

        SET @input = STUFF(@input, PATINDEX('%[^0-9]%', @input), 1, '')

    END


    RETURN @input

END



Usage:

SELECT dbo.fn_KeepOnlyNumbers('A1B2C-3D_4E!5F') AS OnlyNumbers
ago by (1.3k points)

Please log in or register to answer this question.

Welcome to T-SQL.co.uk, where you can ask questions and receive answers from other members of the community.
14 questions
15 answers
0 comments
1,643 users