Sub topic 6
DECODE: Oracle's compact value-matching function
This chapter assumes that you can write basic SELECT statements and understand NULL, NUMBER, and VARCHAR2. DECODE is an Oracle SQL function for comparing one expression with several possible values. It returns the result for the first matching value.
How DECODE works
The general form is:
DECODE(expression,
search1, result1,
search2, result2,
default_result)Oracle compares expression with each search value from left to right. When it finds a match, it returns the corresponding result and stops. If no search value matches, Oracle returns the final default result. If you omit the default, Oracle returns NULL.
SELECT employee_id,
department_id,
DECODE(department_id,
10, 'Administration',
20, 'Marketing',
50, 'Shipping',
'Other') AS department_label
FROM employees;For example, a department ID of 20 produces Marketing. An ID that is not 10, 20, or 50 produces Other.
The first search value also influences how Oracle converts the expression and search values. Keep corresponding values compatible, such as numbers with numbers and character values with character values.
Evaluation order and equality
DECODE tests equality only. It does not directly express conditions such as “salary is greater than 10,000” or “date is within this range.” The order of the search/result pairs matters because Oracle returns the first match.
DECODE is useful when you have a short, fixed lookup list. For more complex conditions, a searched CASE expression is usually clearer.
DECODE and NULL
Unlike ordinary SQL equality, DECODE considers two NULL values equal. This behavior is one of its important differences from a simple CASE expression.
SELECT employee_id,
commission_pct,
DECODE(commission_pct,
NULL, 'No commission',
'Has commission') AS commission_status
FROM employees;If commission_pct is NULL, this expression returns No commission.
The equivalent searched CASE must test NULL explicitly:
CASE
WHEN commission_pct IS NULL THEN 'No commission'
ELSE 'Has commission'
ENDA simple CASE commission_pct WHEN NULL THEN ... does not match a null value because NULL = NULL is not true in normal SQL comparison.
Rewriting DECODE as CASE
Every ordinary value comparison in DECODE can be rewritten as a simple CASE expression:
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;CASE is part of the SQL standard, supports arbitrary conditions, and is usually easier to maintain. DECODE can still be useful in existing Oracle code or for a compact equality lookup.
| Requirement | Usually better choice |
|---|---|
| Fixed equality mapping | `DECODE` or simple `CASE` |
| Comparisons such as `<` or `>=` | Searched `CASE` |
| Portable SQL | `CASE` |
Working around range tests
Because DECODE only compares equal values, you can sometimes transform a condition into a value that DECODE can match. SIGN returns -1, 0, or 1 according to whether a number is negative, zero, or positive.
SELECT employee_id,
salary,
DECODE(SIGN(salary - 10000),
1, 'Above 10000',
'At or below 10000') AS salary_check
FROM employees;This technique is compact, but a searched CASE communicates the rule more directly:
CASE
WHEN salary > 10000 THEN 'Above 10000'
ELSE 'At or below 10000'
ENDExercise
What does DECODE(commissionpct, NULL, 'None', 'Paid') return when commissionpct is NULL, and why?
Show answer
It returns 'None' because DECODE treats a NULL expression and a NULL search value as equal.