我试图从表中的描述字段中删除某些字符串.为此我完成了这个功能
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,' ') 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
我是sql编程的新手,希望对此有所改进.假设我想要一个包含“坏词”列表的表,我想从字符串中删除它并在清理描述时循环遍历它.
我应该指出,这个过程需要尽可能高效,因为我处理的是1500万条记录.
解决方法
你为什么不用
REPLACE?
UPDATE tableName SET columnName = REPLACE(columnName,'specific word','');