CASE expressions allow an expression to be selected based on the evaluation of one or more conditions.
case-expression |--CASE--+-searched-when-clause-+-------------------------------> '-simple-when-clause---' .-ELSE NULL---------------. (1) >--+-------------------------+--END-----------------------------| '-ELSE--result-expression-' searched-when-clause .-----------------------------------------------------. V | |----WHEN--search-condition--THEN--+-result-expression-+-+------| '-NULL--------------' simple-when-clause .-----------------------------------------------. V | |--expression----WHEN--expression--THEN--+-result-expression-+-+--| '-NULL--------------'
In general, the value of the case-expression is the value of the result-expression following the first (leftmost) case that evaluates to true. If no case evaluates to true and the ELSE keyword is present then the result is the value of the result-expression or NULL. If no case evaluates to true and the ELSE keyword is not present then the result is NULL. Note that when a case evaluates to unknown (because of NULLs), the case is not true and hence is treated the same way as a case that evaluates to false.
If the CASE expression is in a VALUES clause, an IN predicate, a GROUP BY clause, or an ORDER BY clause, the search-condition in a searched-when-clause cannot be a quantified predicate, IN predicate using a fullselect, or an EXISTS predicate (SQLSTATE 42625).
When using the simple-when-clause, the value of the expression before the first WHEN keyword is tested for equality with the value of the expression following the WHEN keyword. The data type of the expression before the first WHEN keyword must therefore be comparable to the data types of each expression following the WHEN keyword(s). The expression before the first WHEN keyword in a simple-when-clause cannot include a function that is not deterministic or has an external action (SQLSTATE 42845).
A result-expression is an expression following the THEN or ELSE keywords. There must be at least one result-expression in the CASE expression (NULL cannot be specified for every case) (SQLSTATE 42625). All result expressions must have compatible data types (SQLSTATE 42804).
SELECT EMPNO, LASTNAME,
CASE SUBSTR(WORKDEPT,1,1)
WHEN 'A' THEN 'Administration'
WHEN 'B' THEN 'Human Resources'
WHEN 'C' THEN 'Accounting'
WHEN 'D' THEN 'Design'
WHEN 'E' THEN 'Operations'
END
FROM EMPLOYEE;
SELECT EMPNO, FIRSTNME, MIDINIT, LASTNAME,
CASE
WHEN EDLEVEL < 15 THEN 'SECONDARY'
WHEN EDLEVEL < 19 THEN 'COLLEGE'
ELSE 'POST GRADUATE'
END
FROM EMPLOYEE
SELECT EMPNO, WORKDEPT, SALARY+COMM FROM EMPLOYEE
WHERE (CASE WHEN SALARY=0 THEN NULL
ELSE COMM/SALARY
END) > 0.25;
SELECT LASTNAME,
CASE
WHEN LASTNAME = 'Haas' THEN 'President'
...
SELECT LASTNAME,
CASE LASTNAME
WHEN 'Haas' THEN 'President'
...
There are two scalar functions, NULLIF and COALESCE, that are specialized to handle a subset of the functionality provided by CASE. Table 1 shows the equivalent expressions using CASE or these functions.
Expression | Equivalent Expression |
---|---|
|
NULLIF(e1,e2) |
|
COALESCE(e1,e2) |
|
COALESCE(e1,e2,...,eN) |
|
DECODE(c1,var1, 'a', var2, 'b') |