SQL Server 查找并替换特定列的所有行中的特定单词

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13524021/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 12:17:35  来源:igfitidea点击:

SQL Server find and replace specific word in all rows of specific column

sqlsql-serversql-server-2008

提问by Dev

I have a table TblKitthat has columns Idand Number. Idis primary keyof type intand Numberis varchar(50).

我有一个TblKit包含列IdNumber. Idprimary key类型int并且Numbervarchar(50)

The data in the table looks like this:

表中的数据如下所示:

Id     Number
---    ------
1      KIT001
2      KIT002 
3      DMB001
4      DM002
5      KIT003

I want to replace all the rows of KIT%with CHin number field. The desired output is like

我想KIT%CH数字字段替换所有行。所需的输出就像

Id     Number
---    ------
1      CH001
2      CH002 
3      DMB001
4      DM002
5      CH003

I have tried this update query :

我试过这个更新查询:

UPDATE TblKit SET Number = REPLACE(Number, N'%KIT%', 'CH') 

But however it is not working.

但是,它不起作用。

Can anyone help me regarding this?

任何人都可以帮我解决这个问题吗?

Thank you..

谢谢..

回答by John Woo

UPDATE tblKit
SET number = REPLACE(number, 'KIT', 'CH')
WHERE number like 'KIT%'

or simply this if you are sure that you have no values like this CKIT002

或者只是这个,如果你确定你没有这样的价值观 CKIT002

UPDATE tblKit
SET number = REPLACE(number, 'KIT', 'CH')

回答by wormen22

You can also export the database and then use a program like notepad++ to replace words and then inmport aigain.

也可以导出数据库,然后用notepad++之类的程序替换单词再导入。