UPDATE 和 REPLACE 字符串的一部分 [英] UPDATE and REPLACE part of a string

查看:53
本文介绍了UPDATE 和 REPLACE 字符串的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含两列的表格,IDValue.我想更改第二列中某些字符串的一部分.

I've got a table with two columns, ID and Value. I want to change a part of some strings in the second column.

表格示例:

ID            Value
---------------------------------
1             c:\temp\123\abc\111
2             c:\temp\123\abc\222
3             c:\temp\123\abc\333
4             c:\temp\123\abc\444

现在不需要 Value 字符串中的 123\.我试过 UPDATEREPLACE:

Now the 123\ in the Value string is not needed. I tried UPDATE and REPLACE:

UPDATE dbo.xxx
SET Value = REPLACE(Value, '%123%', '')
WHERE ID <= 4

当我执行脚本时,SQL Server 不报告错误,但也不更新任何内容.这是为什么?

When I execute the script SQL Server does not report an error, but it does not update anything either. Why is that?

推荐答案

REPLACE 中不需要通配符 - 它只找到您为第二个参数输入的字符串,因此以下内容应该工作:

You don't need wildcards in the REPLACE - it just finds the string you enter for the second argument, so the following should work:

UPDATE dbo.xxx
SET Value = REPLACE(Value, '123', '')
WHERE ID <=4

如果要替换的列是 textntext 类型,则需要将其转换为 nvarchar

If the column to replace is type text or ntext you need to cast it to nvarchar

UPDATE dbo.xxx
SET Value = REPLACE(CAST(Value as nVarchar(4000)), '123', '')
WHERE ID <=4

这篇关于UPDATE 和 REPLACE 字符串的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆