Sub topic 8
Choosing between CASE and DECODE, and rewriting DECODE as CASE
Assume you know basic SELECT statements, Oracle data types, NULL, and aggregate functions. This chapter focuses on choosing between the standard SQL CASE expression and Oracle's older DECODE function.
When to choose CASE
Use CASE for new SQL when you need readable, portable conditional logic. It supports two forms:
- A simple
CASEcompares one expression with several values. - A searched
CASEevaluates separate conditions, such as ranges orIS NULL.
CASE can express equality, inequality, ranges, pattern checks, and compound conditions. Its branches are also easy to extend without changing the overall structure.
SELECT employee_id,
salary,
CASE
WHEN salary < 5000 THEN 'Low'
WHEN salary < 10000 THEN 'Medium'
ELSE 'High'
END AS salary_band
FROM employees;The conditions are checked from top to bottom. The first true condition supplies the result. This makes the order important.
When DECODE is useful
DECODE is an Oracle-specific function that compares one expression with a list of values:
DECODE(expression,
search_1, result_1,
search_2, result_2,
default_result)It is compact for a short list of equality mappings, especially in older Oracle code.
SELECT employee_id,
DECODE(department_id,
10, 'Administration',
20, 'Marketing',
50, 'Shipping',
'Other') AS department_label
FROM employees;However, DECODE cannot directly test a range or a general condition. It only compares for equality. It can also be harder for readers who do not know its argument structure.
| Requirement | Better choice |
|---|---|
| Equality mapping of a few values | `DECODE` or simple `CASE` |
| Ranges, `IS NULL`, or compound conditions | Searched `CASE` |
| Portable SQL | `CASE` |
| Maintaining existing Oracle code | Often `DECODE` |
Rewriting DECODE as CASE
A DECODE call maps directly to a simple CASE expression. The expression before the first comma becomes the expression after CASE. Each search/result pair becomes a WHEN/THEN pair. The final argument becomes ELSE.
SELECT employee_id,
CASE department_id
WHEN 10 THEN 'Administration'
WHEN 20 THEN 'Marketing'
WHEN 50 THEN 'Shipping'
ELSE 'Other'
END AS department_label
FROM employees;The following searched CASE is useful when the original logic is more complex than equality:
SELECT employee_id,
CASE
WHEN department_id IN (10, 20) THEN 'Office'
WHEN department_id = 50
AND salary >= 10000 THEN 'Senior shipping'
WHEN department_id = 50 THEN 'Shipping'
ELSE 'Other'
END AS employee_group
FROM employees;The NULL difference
DECODE treats two NULL values as equal. A DECODE expression can therefore match a NULL search value:
SELECT employee_id,
DECODE(commission_pct,
NULL, 'No commission',
'Has commission') AS commission_status
FROM employees;A simple CASE does not match NULL with WHEN NULL, because SQL's NULL = NULL comparison is not true. Rewrite this logic with a searched CASE:
SELECT employee_id,
CASE
WHEN commission_pct IS NULL THEN 'No commission'
ELSE 'Has commission'
END AS commission_status
FROM employees;When converting DECODE, check every NULL search value and replace it with an explicit IS NULL condition.
Data type and maintenance checks
Both constructs can perform implicit data type conversions. Keep comparison values and result values consistent. Mixing numbers and character strings can produce unexpected conversions or errors such as ORA-00932: inconsistent datatypes.
Also check the default behavior. A DECODE without a final default returns NULL when no search matches. A CASE without ELSE does the same. Preserve that behavior during conversion unless you intentionally want a replacement value.
Exercise
How should DECODE(status_code, 'A', 'Active', 'Inactive') be rewritten as CASE?
Show answer
CASE status_code WHEN 'A' THEN 'Active' ELSE 'Inactive' END