我正在尝试从表中的描述字段中删除某些字符串。为此我做了这个功能
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
我是 SQL 编程的新手,希望对此进行改进。假设我想要一个包含“坏词”列表的表,我想从字符串中删除它并在清理描述时循环遍历它。
我应该指出,这个过程需要尽可能高效,因为我要处理 1500 万条记录。