locked
How to write Update sql Query for single column values interchange RRS feed

  • Question

  • User-1552614833 posted

    Hi i ham vaving bellow data in my table

    1  krishna F

    2  ramu    F

    3 yyyy     M

    4 xxxx      M

    I want write a single update query that will update all " F " to "M" and "M" to 'F" in sql server 2008 ?

    can you suggest how to write this query .

     

    Saturday, May 11, 2013 2:50 AM

Answers

  • User1992938117 posted

    Try as:

    UPDATE [MyTable]
    SET GENDER = CASE
                    WHEN GENDER='M' THEN 'F'
                    WHEN GENDER='F' THEN 'M'
                    ELSE GENDER
                 END
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, May 11, 2013 2:56 AM

All replies

  • User1992938117 posted

    Try as:

    UPDATE [MyTable]
    SET GENDER = CASE
                    WHEN GENDER='M' THEN 'F'
                    WHEN GENDER='F' THEN 'M'
                    ELSE GENDER
                 END
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, May 11, 2013 2:56 AM
  • User-578610739 posted

    Hi,

    please check this also,

    http://stackoverflow.com/questions/6035570/sql-conditional-update-query

    Monday, May 13, 2013 1:16 AM
  • User-578610739 posted

    Hi,

    you can give some extra condition also in update commande like.

    UPDATE
        Table
    SET
        A = CASE 
               WHEN (A - B) < 0 THEN
                 C
               WHEN (select sum(col1) > 10 from table1 t1 where t1.col2 = Table.col2 ) 
               ELSE 
                   (A - B)
             END
    where table.col3 = 1 

    Monday, May 13, 2013 1:32 AM
  • User364663285 posted

    Are you using Oracle? If yes, try

    update tab1

    set column3=decode(column3,'F','M','M','F','');

    Saturday, May 18, 2013 3:43 AM