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
  •  | 
  •  

質問

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

正しい解決策はありません

他のヒント

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.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top