user3226286
user3226286

Reputation: 1

How to replace a string in a table

i have a table like below

empid empname grade 
1      rr       A 
2      raju     B 
3      lokesh   A 
4      sathish  B

i want repalce the A by B and B by A in above table using single update statement can any one suggest a good answer in sql..

Upvotes: 0

Views: 62

Answers (2)

Not a bug
Not a bug

Reputation: 4314

Try this query

Update table
set grade = CASE  
               WHEN grade = 'A' THEN 'B' 
               WHEN grade = 'B' THEN 'A' 
            END 
     WHERE grade IN ('A', 'B')

Upvotes: 0

juergen d
juergen d

Reputation: 204784

update your_table
set grade = case when grade = 'A' then 'B'
                 when grade = 'B' then 'A'
            end
where grade in ('A','B')

Upvotes: 2

Related Questions