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