First of all create a function and then after use this function when you need just number from string.
CREATE FUNCTION [dbo].[RemoveAlphaCharacters]
(
@string2number VARCHAR(200)
)
RETURNS VARCHAR(200)
AS
BEGIN
DECLARE @c INT
DECLARE @num VARCHAR(200)
SET @num = ''
DECLARE @txtCurrent CHAR(1)
SET @c = 1
WHILE @c <= LEN(@string2number)
BEGIN
SET @txtCurrent = SUBSTRING(@string2number, @c, 1)
IF ASCII(@txtCurrent) BETWEEN 48 AND 57
SET @num = @num + @txtCurrent
SET @c = @c + 1
END
RETURN @num
END
SELECT dbo.RemoveAlphaCharacters('nikhil1711sangani')
OUTPUT : 1711
CREATE FUNCTION [dbo].[RemoveAlphaCharacters]
(
@string2number VARCHAR(200)
)
RETURNS VARCHAR(200)
AS
BEGIN
DECLARE @c INT
DECLARE @num VARCHAR(200)
SET @num = ''
DECLARE @txtCurrent CHAR(1)
SET @c = 1
WHILE @c <= LEN(@string2number)
BEGIN
SET @txtCurrent = SUBSTRING(@string2number, @c, 1)
IF ASCII(@txtCurrent) BETWEEN 48 AND 57
SET @num = @num + @txtCurrent
SET @c = @c + 1
END
RETURN @num
END
SELECT dbo.RemoveAlphaCharacters('nikhil1711sangani')
OUTPUT : 1711
Comments
Post a Comment