I am trying to remove specific rows from the Description field in a table. To this end, I made this function
CREATE FUNCTION fnDescriptionClean (@strDescription varchar(50)) RETURNS varchar(50) AS BEGIN declare @Return varchar(50) declare @badword varchar(50) set @badword = 'Front' set @strDescription = CASE --Remove from mid string WHEN @strDescription LIKE '% ' + @Badword +' %' THEN REPLACE(@strDescription,' ' + @Badword + ' ',' ') --Remove from start of string WHEN @strDescription LIKE @Badword +' %' THEN RIGHT(@strDescription, (len(@strDescription)-(len(@Badword)+1))) --Remove from end of string WHEN @strDescription LIKE '% ' + @Badword THEN LEFT(@strDescription, (len(@strDescription)-(len(@Badword)+1))) ELSE @strDescription END set @badword = 'Right' set @strDescription = CASE WHEN @strDescription LIKE '% ' + @Badword +' %' THEN REPLACE(@strDescription,' ' + @Badword + ' ',' ') WHEN @strDescription LIKE @Badword +' %' THEN RIGHT(@strDescription, (len(@strDescription)-(len(@Badword)+1))) WHEN @strDescription LIKE '% ' + @Badword THEN LEFT(@strDescription, (len(@strDescription)-(len(@Badword)+1))) ELSE @strDescription END RETURN @strDescription end
I am new to SQL programming and would like to improve this. Suppose I wanted to have a table containing a list of "bad words" that I would like to remove from the line and skip it when clearing the description.
I must point out that this process should be as efficient as possible, as I am dealing with 15 million records.
user1075081
source share