Tuesday, January 2, 2018

Convert Alpha Numeric Characters from String to Integer

Following function keeps only Alphanumeric characters in string and removes all the other character from the string. This is very handy function when working with Alphanumeric String only. I have used this many times.
CREATE FUNCTION dbo.UDF_ParseAlphaChars(@string VARCHAR(8000)
)
RETURNS VARCHAR(8000)AS
BEGIN
DECLARE 
@IncorrectCharLoc SMALLINTSET @IncorrectCharLoc = PATINDEX('%[^0-9A-Za-z]%', @string)WHILE @IncorrectCharLoc > 0BEGIN
SET 
@string = STUFF(@string, @IncorrectCharLoc, 1, '')SET @IncorrectCharLoc = PATINDEX('%[^0-9A-Za-z]%', @string)END
SET 
@string = @stringRETURN @stringENDGO

—-Test
SELECT dbo.UDF_ParseAlphaChars('ABC”_I+{D[]}4|:e;””5,<.F>/?6')GO

No comments :

Post a Comment