How to make multiple update record for a field which can take atmost 3 values in sql

StackOverflow https://stackoverflow.com/questions/22156821

  •  19-10-2022
  •  | 
  •  

Question

I have an employee table, and gender is one of the columns. How can I update the male gender to female and vice-verse?

No correct solution

OTHER TIPS

Depending on how you store the gender column they may be a niftier trick available, but assuming it's a straight forward varchar column the has the values M and F (or male and female - same logic), you could use a case statement:

UPDATE employee
SET    gender = CASE gender WHEN 'M' THEN 'F' WHEN 'F' THEN 'M' ELSE NULL END;

note:
Based on the "at most three values" statement in the OP's title, I'm assuming that nulls are allowed, and should remain unchanged. If this is not true, the case expression could be simplified.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top