0

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..

1
  • 2
    Which database you're working on? Commented Jan 23, 2014 at 5:01

2 Answers 2

2
update your_table
set grade = case when grade = 'A' then 'B'
                 when grade = 'B' then 'A'
            end
where grade in ('A','B')
Sign up to request clarification or add additional context in comments.

1 Comment

@Mr.Radical: Thanks for your concerns. But accepting an answer is totally up to the OP. Please don't ask users to accept something in the future.
0

Try this query

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

1 Comment

This replaces only one case and not both at the same time as the OP wants.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.