SQL CASE Expression
You can use CASE expressions in the select list of SELECT statements. A CASE expression is used like an if-then-else construct to conditionally return a value. Using a CASE expression in the select list of a SELECT statement can help categorize results based on their values or help apply different calculations to a result based on its value.
SELECT empid, CASE state WHEN 'IL' THEN 'in-state' ELSE 'out-of-state' END AS location FROM employee
There are two forms of syntax for CASE expressions: the simple form and searched form.
CASE <common_operand> WHEN <expression> THEN <result> [WHEN <expression> THEN <result> . . .] [ELSE <result>] ENDSyntax of a searched CASE expression
CASE WHEN <condition> THEN <result> [WHEN <condition> THEN <result> . . .] [ELSE <result>] END
When a simple case expression is used, the <common_operand> is compared for equality to each <expression>. If a match is found, the <result> of the matching WHEN <expression> is returned.
When a searched CASE expression is used, each WHEN <condition> must evaluate to true or false. WHEN clauses are evaluated in the order they are defined. The result corresponding to the first true test is returned.
When an ELSE <result> is provided and none of the previous WHEN evaluations match, the ELSE<result> is returned. If an ELSE <result> is omitted and none of the previous WHEN evaluations match, NULL (SQL NULL) is returned.
All <result> values must be of the same data type. Evaluating the result data types is not done until the query is executed. Therefore, it is possible that a bad SELECT statement can be created which later causes an exception to be returned from the node when it processes the bad SELECT statement.
Restrictions on CASE Expressions
- CASE expressions cannot contain functions which use the DISTINCT keyword
- All resulting values of the CASE expression must be of the same data type. Evaluating the result data types is not done until the query is executed. Therefore, it is possible that a bad SELECT statement can be created which later causes an exception to be returned when the query is executed.
- CASE expressions composed of subexpressions which parse but do not make sense does not return an error.
Error Code = Resource limit reached Description = SQL parser stack overflow
Simple CASE Expression
SELECT lastname, CASE gender WHEN ‘M’ THEN ‘Male’ WHEN ‘m’ THEN ‘Male’ WHEN ‘0’ THEN ‘Male’ WHEN ‘F’ THEN ‘Female’ WHEN ‘f’ THEN ‘Female’ WHEN ‘1’ THEN ‘Female’ ELSE ‘Unknown’ END FROM employeesEquality expressions cannot be used to test for NULL so you cannot use a simple CASE expression to test for NULL. You must use a searched CASE expression with IS NULL or IS NOT NULL when testing for NULL.
Searched CASE Expression
SELECT ProductID, Name, ProductNumber, Cost, ListPrice, CASE WHEN ListPrice = 0 THEN NULL ELSE Cost / ListPrice END AS CostOfGoodSold FROM ProductThe searched CASE expression can also be used to test a result column for NULL whereas a simple CASE expression cannot. The following simple CASE expression is invalid:
SELECT CASE middle_name WHEN NULL THEN ‘<NULL>’ ELSE middle_name ENDIn SQL you cannot use equality to test if a column is NULL (empty). Instead you have to use 'IS NULL' as follows:
SELECT CASE WHEN middle_name IS NULL THEN ‘<NULL>’ ELSE middle_name END