Reputation: 1809
I have a table Employee
with following Sample Data
ID Name Gender
1 Mary F
2 John M
3 Smith M
I want to write an Update query that would set Gender to F
where Gender is M
and set Gender to M
where Gender is F
. How can I do this in single update
query?
Upvotes: 1
Views: 2441
Reputation: 11556
We can update by using CASE
expression.
Query
update Employee
set Gender = (
case Gender when 'M' then 'F'
when 'F' then 'M'
else Gender end
);
Upvotes: 1
Reputation: 2379
use case when to update
update employee set Gender=(case when Gender='F' then 'M' else 'f' end)
where gender in ('M','F')
Upvotes: 0
Reputation: 1269483
You would simply use case
:
update t
set Gender = (case when Gender = 'F' then 'M' else 'F' end)
where Gender in ('F', 'M');
Upvotes: 0