简体   繁体   中英

how to exchange values of same column in same table

I want to know how I can exchange values of a same column in the same table itself in one query.

For example, Table is like below.

  SerialNo         Status
      1           Married
      2           Single
      3           Married

Now, Result what i want is that "Married" should be converted into Single and "Single" should be converted into Married.

Expected:

  SerialNo         Status
      1           Single
      2           Married
      3           Single

This should be accomplished in ONE query only. Is it possible to do so with a single query ? If yes, Help me.

Thanks in advance.

UPDATE MyTable
SET Status = (CASE WHEN Status = 'Married' THEN 'Single' ELSE 'Married' END )

Use this:

SET Status = CASE WHEN Status = 'Married' THEN 'Single'
                  WHEN Status = 'Single'  THEN 'Married' 
                                          ELSE 'Unknown' END

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM