-1

Table name: blist

serial_no   city
abcd_086    US
abcd_087    US
abcd_088    US
abcd_089    US
abcd_090    US

Any sql query which can replace abcd_086 ,087, etc to xyz_086,087,etc. Need to replace first 4 char of serial_no column with xyz. Please help!!!

Thom A
  • 88,727
  • 11
  • 45
  • 75
Amit
  • 11
  • 3
  • 2
    Tag spam doesn't help us help you; don't do it. Just tag what is relevant to your question. Speaking of your question, what is it? – Thom A Nov 22 '21 at 12:05

4 Answers4

1

Replace it

update blist
set serial_no = replace(serial_no, 'abcd_', 'xyz_')
where city = 'US'
  and serial_no like 'abcd%'

Demo

LukStorms
  • 28,916
  • 5
  • 31
  • 45
0

the result may vary depending on database. But all of them is close approach. I use postgres db.

UPDATE "blist"
SET "serial_no" = CONCAT('xyz',SUBSTRING("serial_no",5));
ikbalkazanc
  • 161
  • 1
  • 8
0

SQL provides a very helpful string function called REPLACE that allows you to replace all occurrences of a substring in a string with a new substring.

UPDATE students
SET serial_no = REPLACE(serial_no, 'abcd_', 'xyz_');
WHERE city = 'US' and serial_no like 'abcd_%';

SELECT * FROM STUDENT;

Here, you get the Demo

0

ANSI SQL way to replace first 4 char of serial_no column with xyz:

update blist
set serial_no = 'xyz' || substring(serial_no from 5)
jarlh
  • 42,561
  • 8
  • 45
  • 63