Updating column data without using temporary tables
2 answers
You can use the expression case
:
UPDATE emp
SET gender = CASE gender WHEN 'M' THEN 'F' ELSE 'M' END
EDIT:
The above statement assumes for simplicity that 'M'
both 'F'
are the only two parameters - no null
s, unknown, nothing. A more robust query might eliminate this assumption and just strictly replace M
and F
leave the other possible values โโuntouched:
UPDATE emp
SET gender = CASE gender WHEN 'M' THEN 'F'
WHEN 'F' THEN 'M'
ELSE gender
END
+4
source to share