UPDATE SQL statement

Ajit Khodke
Ajit Khodke used Ask the Experts™
on
Hello friends,

I want to update all F (female) and M (Male) values (change all F values to M and vice versa) as per below table.

Id >> Name >> Sex >> Salary
1 >> A >> M >> 6000
2 >> B >> F >> 3500
3 >> C >> M >> 8500
4 >> D >> F >> 600

How should I write the query to update the values?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
SQL Server Data Dude
Most Valuable Expert 2013
Author of the Year 2015
Commented:
Not sure what you mean here.  If the question is 'how do I flip flop values in a column', then give the below a whirl...
UPDATE YourTable
SET Sex = CASE Sex WHEN 'M' THEN 'F' WHEN 'F' THEN 'M' END

Open in new window

If there's any other logic, then please spell it out in greater detail.
bbaoIT Consultant
Commented:
or

UPDATE your_table
SET sex = IIF(sex = 'M', 'F', IIF(sex = 'F', 'M', 'F'))
Jim HornSQL Server Data Dude
Most Valuable Expert 2013
Author of the Year 2015

Commented:
IF is 2012 and above, and I wasn't sure what version of SQL we're on here, so I gave a CASE answer.

Ajit Khodke - Let us know if these solutions met your needs.
Ajit KhodkeDigital Marketing Professional

Author

Commented:
Fixed..
Thanks  Jim and bbao for great solution.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial