Click here to Skip to main content
15,911,531 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
how can I change the data using a single query? I've created one table with columns ID and Gender. I've inserted values and now want to interchange the gender values. What was 'M' shall become 'F' and what was 'F' shall become 'M'.

Id Gender
1 M
2 F
3 M
4 F
5 M
6 F

AFTER CHANGE OUT PUT USE SINGLE QUERY
Id Gender
1 F
2 M
3 F
4 M
5 F
6 M

Below query is not working in SQL server 2005 (SQL Server Express Edition)
XML
<pre lang="SQL">UPDATE dbo.GenderTable
SET gender = CASE
                 WHEN 'F' THEN 'M'
                 WHEN 'M' THEN 'F'
             END</pre>

The error displayed is
'An expression of non-boolean type specified in a context where a condition is expected, near 'THEN'.'
Posted
Updated 5-Oct-11 23:19pm
v2

1 solution

Use
SQL
UPDATE dbo.GenderTable
SET Gender = CASE Gender
               WHEN 'F' THEN 'M'
               WHEN 'M' THEN 'F'
             END


See CASE[^] for description of command.
 
Share this answer
 
v2

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