This function strips a list of characters from the value passed in. Useful for cleaning up input data and removing unwanted characters for streamlined data storage.
2007-10-02 (first published: 2002-06-20)
15,451 reads
This function strips a list of characters from the value passed in. Useful for cleaning up input data and removing unwanted characters for streamlined data storage.
IF EXISTS(SELECT * FROM sysobjects WHERE name = 'fnStripChars') DROP FUNCTION fnStripChars GO CREATE FUNCTION [dbo].[fnStripChars] ( @val varchar(1024), --Value to be stripped @char_list varchar(255) --List of characters to strip from value ) /**************************************************************************************************** This function strips a list of characters from the value passed in. *****************************************************************************************************/RETURNS varchar(1024) AS BEGIN DECLARE @len int, @char char(1) DECLARE @ASCII table(ASC_code int) SET @len = LEN(@char_list) WHILE (@len > 0) BEGIN SET @char = SUBSTRING(@char_list,@len,1) SET @len = @len - 1 INSERT INTO @ASCII SELECT ASCII(@char) END SET @len = LEN(@val) WHILE (@len > 0) BEGIN SET @char = SUBSTRING(@val,@len,1) SET @len = @len - 1 SELECT TOP 1 @val = REPLACE(@val,@char,'') FROM @ASCII WHERE ASCII(@char) IN(SELECT * FROM @ASCII) END RETURN @val END