SQL Server:更新以仅匹配和替换确切的单词

编程入门 行业动态 更新时间:2024-10-20 09:31:00
本文介绍了SQL Server:更新以仅匹配和替换确切的单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想匹配一个确切的单词并将其替换为另一个单词.

I want to match an exact word and replace it with another word.

这是我正在使用的 SQL Server 查询:

Here is the SQL Server query that I am using:

UPDATE BODYCONTENT SET BODY = CAST(REPLACE(CAST(BODY AS NVARCHAR(MAX)), 'Test' , 'prod') AS NTEXT) WHERE BODY COLLATE Latin1_General_CI_AS LIKE '%Test%' COLLATE Latin1_General_CI_AS;

这个查询在做什么:

'Test','test','TEST' 更新为 'prod' -- 这是预期的.

'Test','test','TEST' is updated into 'prod' -- This is expected.

'Test2','TestTest','Fastest' 更新为 'prod' - 我想避免这种行为.

'Test2','TestTest', 'Fastest' is updated into 'prod' - I want to avoid this behavior.

请帮忙.

我尝试过但没有用的其他查询:

Other queries I tried but didn't work:

UPDATE BODYCONTENT SET BODY = CAST(REPLACE(CAST(BODY AS NVARCHAR(MAX)), 'Test' , 'prod') AS NTEXT) WHERE BODY COLLATE Latin1_General_CI_AS LIKE '%[^A-Za-z0-9]Test[^A-Za-z0-9]%' COLLATE Latin1_General_CI_AS;

当我使用以下查询选择测试"时:

And when I do a select for 'Test' using the below query:

SELECT * FROM dbo.BODYCONTENT WHERE CONVERT(NVARCHAR(MAX), BODY) = N'Test';

它没有返回任何东西.但我可以使用以下查询获得结果:

It is not returning anything. But I could get results using the below queries:

SELECT BODY FROM dbo.BODYCONTENT WHERE BODY LIKE '%Test%'; SELECT BODY FROM dbo.BODYCONTENT WHERE BODY COLLATE Latin1_General_CI_AS like '%TEST%' COLLATE Latin1_General_CI_AS;

这是列值:

Test testtest Test1 Test TEST

预期结果:

prod testtest Test1 prod prod

当前结果:

prod prodprod prod1 prod prod

推荐答案

我得到的印象是所有不同版本的 test 都在同一个行值内,这就是你说的原因建议的 like 'test' 不起作用.

I am getting the impression that all the different versions of test are within the same row value, which is why you are stating that the suggested like 'test' is not working.

基于此,以下内容相当难看,但可以满足您的要求:

Based on this, the below is rather ugly but functional per your requirements:

declare @t table(s ntext); insert into @t values('Test testtest Test1 Test TEST'); select s as Original ,ltrim(rtrim(replace( replace( replace(N' ' + cast(s as nvarchar(max)) + N' ' -- Add a single space before and after value, ,' ','<>' -- then replace all spaces with any two characters. ) ,'>test<','>prod<' -- Use these two characters to identify single instances of 'test' ) ,'<>',' ' -- Then replace the delimiting characters with spaces and trim the value. ) ) ) as Updated from @t;

输出:

+-------------------------------+-------------------------------+ | Original | Updated | +-------------------------------+-------------------------------+ | Test testtest Test1 Test TEST | prod testtest Test1 prod prod | +-------------------------------+-------------------------------+

使用 <> 代替空格是由于 SQL Server 的默认行为是忽略字符串比较中的尾随空格.这些可以是任意两个字符,但我觉得这些字符很美观.

The use of <> in place of spaces is due to SQL Server's default behaviour to ignore trailing spaces in string comparisons. These can be any two characters, but I find these to be aesthetically pleasing.

更多推荐

SQL Server:更新以仅匹配和替换确切的单词

本文发布于:2023-11-13 00:18:01,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1582886.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:确切   单词   SQL   Server

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!