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

SqlSql ServerSql Server-2008

Sql Problem Overview


I have a table TblKit that has columns Id and Number. Id is primary key of type int and Number is varchar(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 CH in the Number field. The desired output would look like:

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 it is not working.

Can anyone help me regarding this?

Sql Solutions


Solution 1 - Sql

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

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDevView Question on Stackoverflow
Solution 1 - SqlJohn WooView Answer on Stackoverflow