Strip Characters using STUFF  

Posted by ReelTym


CREATE FUNCTION [dbo].[fn_StripCharacters]
( @String NVARCHAR(MAX),
@MatchExpression VARCHAR(255)
)
RETURNS NVARCHAR(MAX)
AS
BEGIN
SET @MatchExpression = '%['+@MatchExpression+']%'
WHILE PatIndex(@MatchExpression, @String) > 0
SET @String = STUFF(@String, PatIndex(@MatchExpression,@String), 1, '')
RETURN @String
END

SELECT dbo.fn_StripCharacters('a1$%s#22d&39#f4$', '^a-z') as OnlyAlphaChars

This entry was posted on Wednesday, September 15, 2010 and is filed under , . You can leave a response and follow any responses to this entry through the Subscribe to: Post Comments (Atom) .

0 comments