Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysql update one field, replace first character

Tags:

sql

mysql

I have a table name tariff, but now I want to replace values in column service_code

The values are like this:

D2P
D2D
D2D
D2P
D2D

What I want to achieve:

P2P
P2D
P2D
P2P
P2D

Just changing the 'D' to 'P' as first character

like image 449
may Avatar asked Feb 15 '26 09:02

may


2 Answers

Use the below query:

    UPDATE tariff SET service_code=CONCAT('P', SUBSTRING(service_code FROM 2))
    where substring(service_code,1,1)='D';

or

UPDATE tariff SET service_code=CONCAT('P', SUBSTRING(service_code FROM 2))
        where left(service_code,1)='D';
like image 57
lalithkumar Avatar answered Feb 17 '26 02:02

lalithkumar


Use More Generic Query

update tariff set service_code='P'+substring(service_code,2,len(service_code)-1)
where  substring(service_code,1,1)='D'
like image 28
Rohit Kumar Avatar answered Feb 17 '26 01:02

Rohit Kumar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!