Mike Mike - 8 months ago 87
SQL Question

SQL how to swap two specific values from a column

Hi so my question is exactly as it sounds.

I need to swap two values from the column to another value, I know doing

UPDATE TABLE_NAME
SET COLUMN_1 = VALUE
WHERE CONDITION


Works but my problem is that I need to also swap another value to that existing value. In the table below I need to swap baker and baxter, so where ever baxter is, I need it to be baker and where there is baker I need it to be baxter.

ARTISTNAME

BILL
BAKER
JOHN
BAKER
BAXTER
MARY


any help will be greatly appreciated.
Thanks!

Answer Source

Just use case:

update t
    set col = (case when col = 'Baker' then 'Baxter' else 'Baker' end)
    where col in ('Baker', 'Baxter');
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download