Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
203 views
in Technique[技术] by (71.8m points)

sql - 字符串的UPDATE和REPLACE部分(UPDATE and REPLACE part of a string)

I've got a table with two columns, ID and Value .

(我有一个包含两列的表, IDValue 。)

I want to change a part of some strings in the second column.

(我想在第二列中更改某些字符串的一部分。)

Example of Table:

(表示例:)

ID            Value
---------------------------------
1             c:emp123abc111
2             c:emp123abc222
3             c:emp123abc333
4             c:emp123abc444

Now the 123\ in the Value string is not needed.

(现在,不需要Value字符串中的123\ 。)

I tried UPDATE and REPLACE :

(我尝试了UPDATEREPLACE :)

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

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

(当我执行脚本时,SQL Server不会报告错误,但也不会更新任何内容。)

Why is that?

(这是为什么?)

  ask by aston_zh translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

(您不需要在REPLACE通配符-它只需查找您为第二个参数输入的字符串,因此以下代码应该起作用:)

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

(I also added the \ in the replace as I assume you don't need that either)

((我还在替换中添加了\ ,因为我假设您也不需要))


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...