How to use CASE statement in sql select statement to deal with if-then-else logic.

INSERT INTO table_new(newColumnA, newColumnB, newColumnC, newColumnD)
SELECT columnA AS newColumnA,
       columnB AS newColumnB,
       CASE columnC
           WHEN 'value1' THEN 'newValue1'
           WHEN 'value2' THEN 'newValue2'
           ELSE 'newDefaultValue'
       END AS newColumnC,
       columnD AS newColumnD
FROM table_old

See reference for more.

BACK TO TOP
Post tagged with: sql