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 Server REPLACE() Function The REPLACE() function replaces all occurrences of a substring within a string, with a new substring. Note: The search is case-insensitive.
On the Edit menu, point to Find and Replace, and then click Quick Find to open the dialog box with find options, but without replace options. On the Edit menu, point to Find and Replace, and then click Quick Replace to open the dialog box with both find options and replace options.
Update Part of a String in SQL Using the REPLACE() Function We could have done this simpler by using the REPLACE() function. It replaces all occurrences of a specified string value with another string value. The REPLACE function returns a string where it replaces a substring with another substring.
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')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With