Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
Hi,

Suppose I have a table structured as
XML
name     |   gender
 anthony       m
 micheal       m
 jane          f
 kim           f





Now suppose I want to change m to f and at the same time f to m how can
I do so. I want to replace m to f and f to m simultaneously. How can i do so?

The table structure I should get after executing the sql query should be like this after
VB
name     |   gender
 anthony       f
 micheal       f
 jane          m
 kim           m


Please help.
Posted
Updated 2-Dec-10 7:22am
v3

SQL
UPDATE [MyTable]
SET GENDER = CASE
                WHEN GENDER='M' THEN 'F'
                WHEN GENDER='F' THEN 'M'
                ELSE GENDER
             END
 
Share this answer
 
Comments
strogg 3-Dec-10 9:21am    
My vote of 5. That was a better solution. Had never took a look at CASE statements :)
It'd be helpful if you had specified the data type of your 'Gender' field.

If you're using a Bit (assuming 0=male & 1=female OR 1=male & 0=female)
it's simpler - you can use this statement

Update Your_Table_Name SET Gender = ~ Gender

If you're using a string or any other data type, you can do it in multiple steps (I'll assume string here 'm' for male & 'f' for female)

STEP 1 - Temporarily change the 'm' to some other value (say 't')
Update Your_Table SET Gender = 't' where Gender = 'm'

STEP 2 - Change 'f' to 'm'
Update Your_Table SET Gender = 'm' where Gender = 'f'

STEP 3 - Change the temporarily renamed 'm' ('t') to 'f'
Update Your_Table SET Gender = 'f' where Gender = 't'
 
Share this answer
 
Comments
software_Engi08 2-Dec-10 13:26pm    
thanks
Costica U, thanks a lot. You have given my solution also. I was trying it from last one week... Again Thanks a lot.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900