SQL CASE statement
SQL CASE statement allows developers to apply multiple conditions to perform different sets of actions in SQL based on the satisfied conditions. The SQL Case statement should be used when there are more than few conditions needs to be check. If there are 3 or less conditions, you can use the If..Else statement.
The CASE statement evaluates a list of conditions and returns one of multiple possible result expressions.
The CASE expression has two formats,
Both formats support an optional ELSE argument.
CASE statement can be used in any statement or clause that allows a valid expression. For example, you can use CASE in statements such as SELECT, UPDATE, DELETE and SET, and in clauses such as select_list, IN, WHERE, ORDER BY, and HAVING.
If there is no ELSE part and no conditions are true, it returns NULL.
Syntax
- CASE
- WHEN condition1 THEN result1
- WHEN condition2 THEN result2
- WHEN conditionN THEN resultN
- ELSE result
- END;
SELECT statement with a simple CASE expression
A SELECT statement, a simple CASE expression allows for only an equality check -- no other comparisons are made.
The following example uses the CASE expression to change the display of EmployeeDetails to make them more understandable
Syntax
- USE sample;
- GO
- SELECT EmployeeName, EmployeeAddress =
- CASE EmployeeName
- WHEN 'R' THEN 'Rahul'
- WHEN 'M' THEN 'Mountain'
- WHEN 'T' THEN 'Touring'
- WHEN 'S' THEN 'Other sale items'
- ELSE 'Not for Match'
- END,
- EmployeeName
- FROM EmployeeDetails
- ORDER BY EmployeeID;
- GO
Example
SELECT statement with a searched CASE expression
The SELECT statement and the searched CASE expression allow for values to be replaced in the result set based on comparison values.
The following example displays the EmployeeDetails as a text comment based on the EmployeeID for an EmployeeName
Syntax
- SELECT EmployeeID, EmployeeName
- FROM EmployeeDetails
- ORDER BY CASE EmployeeID WHEN 1 THEN EmployeeName END DESC
- ,CASE WHEN EmployeeID = 0 THEN EmployeeName END;
- GO
Example
SELECT CASE in an ORDER BY clause
The following examples uses the CASE expression in an ORDER BY clause to determine the sort order of the rows based on a given column value. In the first example, the value in the EmployeeID, EmployeeName, EmployeeAddress, and column of the EmployeeDetails table is evaluated. Employees that have the EmployeeName set to 1 are returned in order by the EmployeeId in descending order. Employees that have the EmployeeId set to 4 are returned in order by the EmployeeID in ascending order.
In the second example, the result set is ordered by the column EmployeeName,EmployeeAddress when the column EmployeeAddress is equal to EmployeeName and by EmployeeDetails for all other rows
Syntax
- SELECT EmployeeID, EmployeeName, EmployeeAddress From EmployeeDetails
- Where EmployeeName IS NOT NULL ORDER BY CASE EmployeeName WHEN 'Rahul' THEN EmployeeAddress
- ELSE EmployeeName End
Example
CASE in an UPDATE statement
The following example uses the CASE expression in an UPDATE statement to determine the value that is set for the column EmployeeName for EmployeeDetails with EmployeeName set to EmployeeDetails.
Syntax
- UPDATE EmployeeDetails
- SET EmployeeName = CASE EmployeeAddress
- WHEN 'RA' THEN 'Rahul'
- WHEN 'Sa' THEN 'YashBeniwal'
- ELSE NULL
- END
Example
SELECT CASE in a SET statement
The SELECT CASE uses the CASE expression in a SET statement in the table-valued function EmployeeDetails. In the Sample database, all data related to people is stored in the EmployeeDetails table.
Syntax
- UPDATE EmployeeDetails
- SET EmployeeName = CASE EmployeeAddress
- WHEN 'RA' THEN 'Ravi'
- WHEN 'Sa' THEN 'Singh'
- ELSE NULL
- END
CASE in a HAVING clause
The following example uses the CASE expression in a HAVING clause to restrict the rows returned by the SELECT statement.
The statement returns the EmployeeName in the EmployeeDetails table
Syntax
- USE sample ;
- GO
- SELECT EmployeeName,EmployeeAddress, EmployeeCity
- FROM EmployeeDetails AS E
- JOIN EmployeeDetail AS ph1 ON EmployeeID=EmpId
- GROUP BY EmployeeName HAVING(EmployeeName='M' WHEN ph1.EmployeeName)
- THEN Ph1.EmployeeName
- ORDER BY EmployeeID DESC;
Summary
In this tutorial, we learned the basics of the SQL SELECT CASE statement.