Wednesday, March 28, 2012

Find out a character or Number from string.

Following SQL User Defined Function will extract/parse numbers from the string.

 

CREATE FUNCTION ExtractInteger(@String VARCHAR(2000))

RETURNS VARCHAR(1000)

AS

BEGIN

DECLARE @Count INT

DECLARE @IntNumbers VARCHAR(1000)

SET @Count = 0

SET @IntNumbers = ''

 

WHILE @Count <= LEN(@String)

BEGIN

IF SUBSTRING(@String,@Count,1) between '0' and '9'

BEGIN

SET @IntNumbers = @IntNumbers + SUBSTRING(@String,@Count,1)

END

SET @Count = @Count + 1

END

RETURN @IntNumbers

END

GO

 

Execute this

SELECT dbo.ExtractInteger('My birthday date 06-06-1987')GO

No comments:

Post a Comment