Saturday, December 12, 2009

Convert male into female and vice versa

tblEmployee(EmployeeID, EmployeeName, Gender, etc.)
Gender contains two values only, 'M' represents for 'Male' and 'F' represent for 'Female'.
For some reason, after importing employee data, you found that Gender field values have been reversed and you need to convert them.
UPDATE dbo.tblEmployee
SET Gender = t2.Gender
FROM dbo.tblEmployee t1
INNER JOIN (SELECT EmployeeID,
CASE Gender
WHEN 'M' THEN 'F'
WHEN 'F' THEN 'M'
END AS Gender
FROM dbo.tblEmployee) t2 ON t1.EmployeeID = t2.EmployeeID

No comments:

Post a Comment