Introduction
The CASE Statement is used for evaluating a list of conditions to return a result. This T-SQL Expression begins with the keyword CASE. The CASE statement can be used in similar fashion to IF/ELSEIF, however the CASE statement is often much more readable in code. A CASE Statement can be used in the SELECT as well in other sections of a query such as WHERE, GROUP BY and ORDER BY. The CASE Statement has a few basic parts:
-
CASE [input_expression]
-
WHEN condition
THEN result
[..n]
-
[ELSE] result
-
END
following the 'CASE' keyword you may put an optional input expression that can be used for evaluation. An example will be shown below.
Beginning Example
This example will use the following table:
SELECT Name, Position, Salary
FROM Employees
Here is the resultset from the above SELECT query:
| Name | Position | Salary |
| Joe Grape | Manager | 80000 |
| John Plum | Software Developer | 65000 |
| Frank Apple | Software Developer | 62000 |
| Patty Pineapple | Software Developer | 60000 |
| Judy Peach | Software Developer | 50000 |
| Jane Orange | Project Manager | 75000 |
Using the CASE expression we can evaluate the value of columns and create new return column with a different value. In this example we will return a silly name for some of the Positions from the above Employees table.
SELECT
Name,
CASE
WHEN [Position] = 'Manager'
THEN'Bossman'
WHEN [Position] = 'Software Developer'
THEN 'Geek'
ELSE
'Other'
END as SillyPositionName
FROM
Employees
Here is the resultset from the above CASE Statement in the SELECT query from the Employees table:
| Name | SillyPositionName |
| Joe Grape | Bossman |
| John Plum | Geek |
| Frank Apple | Geek |
| Patty Pineapple | Geek |
| Judy Peach | Geek |
| Jane Orange | Other |
Using CASE input_expression Example
This example will use the following table:
SELECT Name, Position, Gender
FROM Employees
Here is the resultset from the above SELECT query:
| Name | Position | Gender |
| Joe Grape | Manager | 1 |
| John Plum | Software Developer | 1 |
| Frank Apple | Software Developer | 1 |
| Patty Pineapple | Software Developer | 0 |
| Judy Peach | Software Developer | 0 |
| Jane Orange | Project Manager | 0 |
Using the CASE expression we can evaluate the value of a column(s) and create new return column with a different value. In this example we will return the Gender description (0=Female,1=Male) for the Gender column from the above Employees table.
SELECT
Name,
CASE [Gender]
WHEN 0
THEN 'Female'
WHEN 1
THEN 'Male'
ELSE
'Other'
END as GenderDescription
FROM
Employees
Here is the resultset from the above CASE Statement in the SELECT query from the Employees table:
| Name | GenderDescription |
| Joe Grape | Male |
| John Plum | Male |
| Frank Apple | Male |
| Patty Pineapple | Female |
| Judy Peach | Female |
| Jane Orange | Female |
Range Example
This example will use the following table:
SELECT Name, Position, Salary
FROM Employees
Here is the resultset from the above SELECT query:
| Name | Position | Salary |
| Joe Grape | Manager | 80000 |
| John Plum | Software Developer | 65000 |
| Frank Apple | Software Developer | 62000 |
| Patty Pineapple | Software Developer | 60000 |
| Judy Peach | Software Developer | 50000 |
| Jane Orange | Project Manager | 75000 |
Using the CASE expression we can evaluate the value of columns and create new return column with a different value. In this example we will return a Salary Range for the employee Salaries from the above Employees table.
SELECT
Name,
CASE
WHEN [Salary] >= 75000
THEN'High'
WHEN [Salary] >= 60000
THEN 'Middle'
ELSE
'Low'
END as SalaryRange
FROM
Employees
Here is the resultset from the above CASE Statement in the SELECT query from the Employees table:
| Name | SalaryRange |
| Joe Grape | High |
| John Plum | Middle |
| Frank Apple | Middle |
| Patty Pineapple | Middle |
| Judy Peach | Low |
| Jane Orange | High |
|