如何在不替换子字符串的情况下替换句子中的特定单词 [英] How to replace a specific word in a sentence without replacing in substring in SQL Server
本文介绍了如何在不替换子字符串的情况下替换句子中的特定单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想替换句子中的特定单词,而不是子字符串。
I want to replace a specific word in a sentence but not in a substring.
喜欢
DECLARE @i VARCHAR(250)
SET @i = 'MR JOHN NAMR is working from 3 days.'
PRINT REPLACE(@i, 'MR ', '')
输出:
'JOHN NAis working from 3 days.'
就像在此我想用一个空字符串替换'MR'。但是,如果您看到它也正在取代 NAMR中的 MR。
Like in this I want to replace 'MR ' with an empty string. But if you see it is replacing 'MR ' from 'NAMR ' as well.
请提出一个解决方案。
Kindly suggest a solution.
推荐答案
您可以尝试以下查询。
You can try the following query. Here at the start and end a space has been inserted first and then removed that space.
DECLARE @i VARCHAR(250)
SET @i = 'MR JOHN NAMR is working from 3 days.'
SELECT rtrim(ltrim(Replace(replace(' '+@i+' ',' MR ',' '),
' MR ',' ')))
输出如下所示
JOHN NAMR从3天开始工作。
JOHN NAMR is working from 3 days.
您可以找到实时演示 Demo Replace Word
You can find the live demo Demo Replace Word
这篇关于如何在不替换子字符串的情况下替换句子中的特定单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文