nik nik - 22 days ago 5
SQL Question

I have a small registration Form with 4 fields

The fields are

employeeID int
name varchar
Gender int (0 = male, 1 = female)
Date datetime


Now if i am writing select query like this

select employeeID,name,Gender,Date
from empTable
where employeeID=@id


I get result

'101','cccc','1','11/1/2014'


But i need a query its give me

'101','cccc','Female','11/1/2014'

Answer

If this is Sql Server you can use a CASE statement.

select employeeID
  ,name
  ,case when Gender = 1 then 'Female' else 'Male' end AS Gender
  ,Date 
from empTable 
where employeeID=@id

IIF statement

select employeeID
  ,name
  , IIF(Gender = 1, 'Female', 'Male') AS Gender
  ,Date 
from empTable 
where employeeID=@id