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