Case statement in sql Server


Case expression can be used in Sql. This can be used with SELECT list, WHERE clause, HAVING clauses, IN list, DELETE and UPDATE statements.

Case is used to check one Expression over multiple values.

Syntax:

CASE Expression
WHEN expression1 THEN value1
WHEN expression2 THEN value2
ELSE value3
END

Example:

Consider a situation where you have to show EmployeeID and EmployeeName , so that if EmployeeID is 1 then the name should be 'John', when EmployeeID is 2 then the name should be 'Smith' and when the EmployeeID is 3, then the name should be 'Adam'. It can be done using CASE as below...

Select EmployeeID , EmployeeName =
CASE EmployeeID
WHEN  1 THEN 'John'
WHEN 2 THEN 'Smith'
WHEN 3 THEN 'Adam'
ELSE 'Something'
END From EmployeeTable.

For More Information :Case statement in Sql

No comments:

Post a Comment