Thursday, August 09, 2007

SQL Function to Parse AlphaNumeric Characters from String

CREATE FUNCTION dbo.ParseAlphaCharsByString
(
@text VARCHAR(8000)
)
RETURNS VARCHAR(8000)
AS
BEGIN
DECLARE @IncorrChachIndex SMALLINT

SET @IncorrChachIndex = PATINDEX('%[^0-9A-Za-z]%', @text)
WHILE @IncorrChachIndex > 0
BEGIN
SET @text = STUFF(@text, @IncorrChachIndex, 1, '')
SET @IncorrChachIndex = PATINDEX('%[^0-9A-Za-z]%', @text)
END
RETURN @text
END
GO

No comments: