UPDATE SQL statement

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?
LVL 2
Ajit KhodkeDigital Marketing ProfessionalAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
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.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
bbaoIT ConsultantCommented:
or

UPDATE your_table
SET sex = IIF(sex = 'M', 'F', IIF(sex = 'F', 'M', 'F'))
0
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
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.
0
Ajit KhodkeDigital Marketing ProfessionalAuthor Commented:
Fixed..
Thanks  Jim and bbao for great solution.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.