may
may

Reputation: 755

mysql update one field, replace first character

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

Upvotes: 3

Views: 4207

Answers (2)

lalithkumar
lalithkumar

Reputation: 3540

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';

Upvotes: 6

Rohit Kumar
Rohit Kumar

Reputation: 806

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'

Upvotes: 3

Related Questions