MS SQL SERVER 2014中的字符串修改/过滤

时间:2015-09-10 19:49:35

标签: sql sql-server sql-server-2008 tsql sql-server-2014

以下是输入字符串:

声明@inputvalue varchar(max)

设置@inputvalue ='布鲁塞尔芽菜是一种绿色素食,你应该毫无疑问地蒸发掉。蒸 萌芽 只会使您的免疫力得到提升。  原始的 Sproutbrussels 布鲁塞尔 仍然具有降低胆固醇的能力,但与蒸煮的布鲁塞尔芽菜一样多。同样,以下是蒸汽 brusselssprout “的其他一些健康益处 我喜欢 sprout ,而不是 布鲁塞尔

要求:

如果它们彼此不相邻,则需要删除sprout和Brussels这两个词,即

1)如果“布鲁塞尔”不在其附近(左或右,有或没有空格),则需要删除“萌芽”字样 2)如果“萌芽”不在其附近(左侧或右侧,有或没有空间),需要删除“布鲁塞尔”字样 3)即使它们之间有多个空格,也要认为这些词是相邻的。
布鲁塞尔萌芽 - 不应从字符串中删除 Brusselssprout - 不应该从字符串中删除。
Sproutbrussels - 不应该从字符串中删除。

将删除 粗体斜体 中突出显示的字词,并且不会删除粗体中突出显示的字词。

注意:整个输入字符串可以不同/单词之间可能根本不包含任何空格。因此,不能通过分割空格或任何其他分隔符来完成。

预期输出字符串:

布鲁塞尔豆芽是一种绿色蔬菜,你应该毫无疑问地蒸发掉。蒸你的意志只能增强你的免疫力。原始 Sproutbrussels 或仍然具有降低胆固醇的能力,但与蒸煮的布鲁塞尔发芽一样多。同样,以下是蒸
brusselssprout “的一些其他健康益处。 我喜欢,不是“

1 个答案:

答案 0 :(得分:0)

这可能不是很有效但它有效:

Declare @inputvalue varchar(max)

Set @inputvalue = 'Brussels sprout is one type of green veggie you should simply steam away without a doubt. Steaming your sprout will only enable a build up on your immunity. Raw Sproutbrussels or Brussels still has the cholesterol-lowering ability but as much as steamed brussels           sprout. Likewise, here are some of the other health benefits of steaming brusselssprout ".
I like sprout, not Brussels.';

declare @word1 nvarchar(100) = 'brussels';
declare @word2 nvarchar(100) = 'sprout ';

with W1(id, pos, val) as (
    Select 1, CHARINDEX(@word1, @inputvalue, 1)+1, 
        Case When
            right(rtrim(left(@inputvalue, CHARINDEX(@word1, @inputvalue, 1)-1)), len(@word2)) = @word2 
            or left(ltrim(right(@inputvalue, len(@inputvalue) - CHARINDEX(@word1, @inputvalue, 1) - len(@word1) +1)), len(@word2)) = @word2 
        then @inputvalue else 
            left(@inputvalue, CHARINDEX(@word1, @inputvalue, 1) - 1)
        end
    Union All
    Select id+1, CHARINDEX(@word1, val, pos)+1, 
        Case When
            right(rtrim(left(val, CHARINDEX(@word1, val, pos)-1)), len(@word2)) = @word2 
            or left(ltrim(right(val, len(val) - CHARINDEX(@word1, val, pos) - len(@word1) +1)), len(@word2)) = @word2 
        then val else 
            left(val, CHARINDEX(@word1, val, pos) - 1)
            + right(val, len(val) - len(@word1) - CHARINDEX(@word1, val, pos) + 1) 
        end
    From w1     
    Where CHARINDEX(@word1, val, pos) > 0
), W2(id, pos, val) as (
    Select 1, CHARINDEX(@word2, val, 1)+1, 
        Case When
            right(rtrim(left(val, CHARINDEX(@word2, val, 1)-1)), len(@word1)) = @word1 
            or left(ltrim(right(val, len(val) - CHARINDEX(@word2, val, 1) - len(@word2) +1)), len(@word1)) = @word1 
        then val else 
            left(val, CHARINDEX(@word2, val, 1) - 1)
        end
    From ( 
        Select val From w1 Where id in (Select max(id) From w1)
    ) as w
    Union All
    Select id+1, CHARINDEX(@word2, val, pos)+1, 
        Case When
            right(rtrim(left(val, CHARINDEX(@word2, val, pos)-1)), len(@word1)) = @word1 
            or left(ltrim(right(val, len(val) - CHARINDEX(@word2, val, pos) - len(@word2) +1)), len(@word1)) = @word1 
        then val else 
            left(val, CHARINDEX(@word2, val, pos) - 1)
            + right(val, len(val) - len(@word2) - CHARINDEX(@word2, val, pos) + 1) 
        end
    From w2 
    Where CHARINDEX(@word2, val, pos) > 0
)
Select val From w2 Where id in (Select max(id) From w2)

Option (maxrecursion 0)

CTE W1也可以放在一个函数中:

with W1(id, pos, val) as (
    Select 1, CHARINDEX(@word1, @inputvalue, 1)+1, 
        Case When
            right(rtrim(left(@inputvalue, CHARINDEX(@word1, @inputvalue, 1)-1)), len(@word2)) = @word2 
            or left(ltrim(right(@inputvalue, len(@inputvalue) - CHARINDEX(@word1, @inputvalue, 1) - len(@word1) +1)), len(@word2)) = @word2 
        then @inputvalue else 
            left(@inputvalue, CHARINDEX(@word1, @inputvalue, 1) - 1)
        end
    Union All
    Select id+1, CHARINDEX(@word1, val, pos)+1, 
        Case When
            right(rtrim(left(val, CHARINDEX(@word1, val, pos)-1)), len(@word2)) = @word2 
            or left(ltrim(right(val, len(val) - CHARINDEX(@word1, val, pos) - len(@word1) +1)), len(@word2)) = @word2 
        then val else 
            left(val, CHARINDEX(@word1, val, pos) - 1)
            + right(val, len(val) - len(@word1) - CHARINDEX(@word1, val, pos) + 1) 
        end
    From w1     
    Where CHARINDEX(@word1, val, pos) > 0
)
Select val From w1 Where id in (Select max(id) From w1)

然后你只召唤两次:

Set @newvalue = replaceWords(replaceWords(@inputvalue, 'brussels', 'sprout'), 'sprout', 'brussels')

请注意,我喜欢布鲁塞尔豆芽! :)