使用 ms sql server“替换"替换整个单词 [英] Replace whole word using ms sql server "replace"
本文介绍了使用 ms sql server“替换"替换整个单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
declare @str varchar(50)='GoodLuck Markand'
declare @replacedString varchar(50)
set @replacedString = replace(@str,'Good','Better')
print @replacedString
我希望输出保持不变,因为没有像好"这样的单词.但输出是BetterLuck Markand".
I want the output to be unchanged, because there is no single word like 'Good'. But the output is 'BetterLuck Markand'.
我希望 MS SQL 替换功能比较确切的单词,然后替换它.我该怎么做?
I want the MS SQL Replace function to Compare the exact word and then replace it. How can i do so?
推荐答案
试试这个:(按预期工作)
Try This: (Works as Expected)
declare @str varchar(500)
set @str = 'Good Good Good Good Good Good Good Good Luck Good GoodLuck MarkAndGood GoodMarkAnd MarkAndGood Good'
declare @replacedString varchar(500)
SET @replacedString = replace(@str,' Good ',' Better ')
SET @replacedString = replace(@replacedString,' Good ',' Better ')
SET @replacedString = CASE WHEN CHARINDEX('Good ', @replacedString) = 1 THEN
STUFF(@replacedString, 1, Len('Good'), 'Better')
ELSE @replacedString END
SELECT CASE WHEN CHARINDEX(REVERSE(' Good'), REVERSE(@replacedString)) = 1 THEN
Reverse(STUFF(Reverse(@replacedString), CHARINDEX(Reverse(' Good'),
Reverse(@replacedString)), Len(' Good'), Reverse(' Better')))
ELSE @replacedString END AS A
输入:
祝你好运好运好运
结果:
好运好运好运好运好
这篇关于使用 ms sql server“替换"替换整个单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文