Technical Article

Allow Only Numbers

,

SELECT dbo.fnc_AllowNumbersOnly('ST-3')

returns 3

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

-- SELECT dbo.fnc_AllowNumbersOnly('ST-3')

CREATE FUNCTION [dbo].[fnc_AllowNumbersOnly](
@string NVARCHAR(60)
)
RETURNS NVARCHAR(60)
AS
 BEGIN
-- Strip @number of extra chracters
DECLARE @lenPhone INT ,@phoneStr NVARCHAR(30)
WHILE PATINDEX('%[^0-9]%', @string) > 0 
 SET @string = REPLACE(@string, 
 SUBSTRING(@string,PATINDEX('%[^0-9]%', @string),1),'') 
-- Returns Value 
 RETURN @string
END

Rate

5 (2)

You rated this post out of 5. Change rating

Share

Share

Rate

5 (2)

You rated this post out of 5. Change rating