Sub topic 8
Choosing between CASE and DECODE, and rewriting DECODE as CASE
This chapter assumes you can write a simple CASE, a searched CASE and a basic DECODE call. It also assumes you know how NULL behaves in comparisons. Here you will learn how to decide between the two and how to convert a DECODE call to CASE without changing its results.
The short answer
Use CASE for new code. DECODE still works and is common in older Oracle code, so you need to read it and convert it safely.
| Aspect | CASE | DECODE |
|---|---|---|
| Standard | ANSI SQL, portable | Oracle only |
| Comparisons | Any condition in a searched CASE | Equality only |
| NULL matching | `WHEN NULL` never matches. Use `IS NULL`. | Matches NULL to NULL |
| Type handling | Strict. Mixed result types raise ORA-00932. | Lenient. Converts implicitly, based on the first search value and first result. |
| Readability | Keywords make each branch clear | Positional arguments, hard to read when long |
| Use in PL/SQL | Statement and expression forms | Mainly for SQL statements |
DECODE has one real advantage: it is short for a small equality test, and it treats NULL as a matchable value. Everything else favors CASE.
Rewriting DECODE as CASE
A DECODE call has this shape:
DECODE(expr, search1, result1, search2, result2, ..., default)It means "compare expr to each search value in order and return the matching result, or the default."
Rule 1: non-NULL search values become a simple CASE
When no search value is NULL, the rewrite is mechanical. This is the DECODE from earlier in the tutorial and its equivalent:
SELECT employee_id,
DECODE(department_id,
10, 'Administration',
20, 'Marketing',
50, 'Shipping',
'Other') AS decode_label,
CASE department_id
WHEN 10 THEN 'Administration'
WHEN 20 THEN 'Marketing'
WHEN 50 THEN 'Shipping'
ELSE 'Other'
END AS case_label
FROM employees;Both columns return the same values. The last DECODE argument becomes the ELSE branch. If the DECODE call has an even number of arguments after expr, it has no default and returns NULL when nothing matches. A CASE without an ELSE also returns NULL, so you add no ELSE.
Rule 2: a NULL search value needs IS NULL
DECODE matches NULL to NULL. A simple CASE does not, because it compares with =, and NULL = NULL is not true. A direct translation silently changes the result:
SELECT employee_id,
commission_pct,
DECODE(commission_pct, NULL, 'No commission', 'Has commission') AS decode_result,
CASE commission_pct
WHEN NULL THEN 'No commission'
ELSE 'Has commission'
END AS wrong_case_result,
CASE
WHEN commission_pct IS NULL THEN 'No commission'
ELSE 'Has commission'
END AS right_case_result
FROM employees;wrong_case_result is always 'Has commission', even for rows with no commission. The query raises no error. This is the most common mistake when converting DECODE, and the compiler cannot catch it.
Rule 3: mixed NULL and value searches become a searched CASE
When a DECODE has both NULL and non-NULL search values, use a searched CASE. Keep the branches in the same order as the DECODE pairs:
SELECT employee_id,
DECODE(commission_pct,
NULL, 'None',
0.1, 'Low',
0.2, 'Medium',
'Other') AS decode_band,
CASE
WHEN commission_pct IS NULL THEN 'None'
WHEN commission_pct = 0.1 THEN 'Low'
WHEN commission_pct = 0.2 THEN 'Medium'
ELSE 'Other'
END AS case_band
FROM employees;Order matters in both forms. Both return the first match, so if a search value appears twice, the first pair wins in each.
Rule 4: DECODE with SIGN becomes a plain comparison
Developers used DECODE(SIGN(...)) because DECODE cannot test ranges. CASE can, so the workaround disappears:
SELECT employee_id,
salary,
DECODE(SIGN(salary - 10000), 1, 'Above 10000', 'At or below 10000') AS decode_check,
CASE
WHEN salary > 10000 THEN 'Above 10000'
ELSE 'At or below 10000'
END AS case_check
FROM employees;SIGN returns 1, 0 or -1. A NULL salary gives SIGN(NULL), which is NULL. It matches no search value, so DECODE returns the default. In the CASE version, salary > 10000 is unknown for NULL, so the branch is skipped and the ELSE applies. The results agree here, but only because the ELSE catches both situations.
Conversion checklist
- Find any NULL in the search positions. Each one becomes
WHEN expr IS NULL. - If any NULL search exists, use a searched CASE. Otherwise use a simple CASE.
- Turn the final unpaired argument into ELSE. If there is none, add no ELSE.
- Replace
SIGN-based tricks with direct comparisons. - Check the data types of the result (see the next section).
- Compare the old and new results on real data.
Data type differences that can break a conversion
DECODE converts types implicitly. It treats the first result as the guide: if the first result is a character value, the other results are converted to VARCHAR2. CASE requires the result expressions to be compatible.
-- Works: the number 5 is converted to the string '5'
SELECT DECODE(department_id, 10, 'Admin', 20, 5, 'Other') AS result
FROM employees;
-- Fails with ORA-00932: inconsistent data types
SELECT CASE department_id
WHEN 10 THEN 'Admin'
WHEN 20 THEN 5
ELSE 'Other'
END AS result
FROM employees;After a conversion, an ORA-00932 error often points to a hidden implicit conversion in the original code. Fix it by making the types explicit, for example TO_CHAR(5). DECODE also compares expr to each search value after converting to the type of the first search value. If you compare a VARCHAR2 column to a number, rows with non-numeric text can raise ORA-01722. A simple CASE has stricter rules, so check this when you convert.
Proving the rewrite is equivalent
Test the conversion instead of trusting your reading. MINUS treats two NULLs as equal, so it is a good comparison tool. Run it in both directions:
SELECT employee_id,
DECODE(commission_pct, NULL, 'None', 0.1, 'Low', 'Other') AS label
FROM employees
MINUS
SELECT employee_id,
CASE
WHEN commission_pct IS NULL THEN 'None'
WHEN commission_pct = 0.1 THEN 'Low'
ELSE 'Other'
END
FROM employees;Then swap the two queries. If both return no rows, the expressions match on the data you have. Include rows with NULLs and edge values in the data you test.
When to keep DECODE
Converting everything is not always worth it. Leave DECODE alone when:
- The code works, is tested, and is short, such as
DECODE(flag, 'Y', 1, 0). - You would be changing many files with no behavior change, which adds review cost and risk.
- You are fixing an unrelated bug and a conversion would hide the real diff.
Convert when you edit the expression anyway, when it has more than a few branches, when it needs a range, or when it must run on another database.
Choosing between CASE and the NULL functions
If the only question is about NULL, a shorter function usually reads best. NVL(commission_pct, 0) is clearer than a three-line CASE. COALESCE takes several arguments and is standard SQL. Use CASE once the logic includes more than a NULL replacement.
Exercise
Convert DECODE(job_id, 'AD_PRES', 'Executive', 'AD_VP', 'Executive', NULL, 'Unassigned', 'Staff') to a CASE expression with the same results. Which form do you need, simple or searched, and why?
Show answer
Use a searched CASE, because one search value is NULL and a simple CASE never matches NULL. The result is CASE WHEN job_id = 'AD_PRES' THEN 'Executive' WHEN job_id = 'AD_VP' THEN 'Executive' WHEN job_id IS NULL THEN 'Unassigned' ELSE 'Staff' END. You can shorten the first two branches to WHEN job_id IN ('AD_PRES', 'AD_VP').
Exercise
A colleague converts DECODE(status, NULL, 'Unknown', 'Known') to CASE status WHEN NULL THEN 'Unknown' ELSE 'Known' END. The query runs without errors. What goes wrong, and how do you fix it?
Show answer
The simple CASE compares with =, and a comparison with NULL is never true. Every row returns 'Known', including rows where status is NULL. Fix it with CASE WHEN status IS NULL THEN 'Unknown' ELSE 'Known' END.
Exercise
A converted query now raises ORA-00932, although the original DECODE ran fine. It returns 'Admin' for one branch and the number 5 for another. Why did DECODE succeed, and what is the fix?
Show answer
DECODE converted the number to a string because its first result is a character value. CASE requires compatible result types and does not do that conversion. Make the type explicit, for example TO_CHAR(5), or return a value of the same type from every branch.