CASE expressions
A CASE expression is a conditional expression, which is similar to the concept of the CASE statement in programming languages. You can use a CASE expression when you want to change the way data is represented. The CASE expression allows a statement to return one of several possible results, depending on which of several condition tests evaluates to TRUE.
TEXT or BYTE values are not allowed in a CASE expression.
Consider a column that represents marital
status numerically as 1,2,3,4
with the corresponding
values meaning single, married, divorced, widowed. In some cases,
you might prefer to store the short values (1,2,3,4
)
for database efficiency, but employees in human resources might prefer
the more descriptive values (single, married, divorced, widowed).
The CASE expression makes such conversions between different sets
of values easy.
In , the CASE expression also supports extended data types and cast expressions.
SELECT
CASE
WHEN manu_code = "HRO" THEN "Hero"
WHEN manu_code = "SHM" THEN "Shimara"
WHEN manu_code = "PRC" THEN "ProCycle"
WHEN manu_code = "ANZ" THEN "Anza"
ELSE NULL
END
FROM stock;
You must include at least one WHEN clause within the CASE expression; subsequent WHEN clauses and the ELSE clause are optional. If no WHEN condition evaluates to true, the resulting value is NULL. You can use the IS NULL expression to handle NULL results. For information on handling NULL values, see the HCL OneDB™ Guide to SQL: Syntax.
For information about how to use the CASE expression to update a column, see CASE expression to update a column.