Sub topic 12
Range checks in DECODE with SIGN
This chapter assumes you know how DECODE compares an expression with search values and how it treats NULL. You should also be comfortable reading searched CASE. DECODE tests equality only, so the chapter shows how to express "greater than" and "less than" with it. It also covers where the technique breaks and when to use CASE instead.
Why DECODE cannot test ranges directly
DECODE compares its first argument with each search value using equality. There is no way to write DECODE(salary, > 10000, ...). A searched CASE handles ranges with WHEN salary > 10000, but DECODE needs the range turned into a value it can match.
The SIGN function does that. It returns a number that tells you the sign of its argument:
| Argument | SIGN result |
|---|---|
| Negative | -1 |
| Zero | 0 |
| Positive | 1 |
| NULL | NULL |
You can check these values yourself:
SELECT SIGN(-5) AS negative,
SIGN(0) AS zero,
SIGN(12.5) AS positive,
SIGN(NULL) AS null_input
FROM dual;If you subtract a threshold from a value, the sign of the difference tells you which side of the threshold the value is on. A comparison has become one of three outcomes (-1, 0 or 1), and DECODE can match those.
The basic pattern
SELECT employee_id,
salary,
DECODE(SIGN(salary - 10000),
1, 'Above 10000',
'At or below 10000') AS salary_check
FROM employees;Here is how it evaluates:
salary - 10000is positive when the salary is above the threshold, so SIGN returns 1.- DECODE matches 1 and returns
'Above 10000'. - For a difference of 0 or -1, no search value matches, so the default
'At or below 10000'is returned.
Mapping comparisons to search values
Every comparison operator can be written as a SIGN test against a threshold t. The table shows how to build each one.
| Condition on x | Search value(s) in `DECODE(SIGN(x - t), ...)` | Notes |
|---|---|---|
| x > t | 1 | Everything else goes to the default |
| x >= t | -1 returns the "no" result, default returns the "yes" result | Or list 0 and 1 as two separate pairs |
| x < t | -1 | |
| x <= t | 1 returns the "no" result, default returns the "yes" result | Or list -1 and 0 as separate pairs |
| x = t | 0 | Plain DECODE(x, t, ...) is simpler |
Inclusive boundaries are the most common source of mistakes. When the condition is x >= t, the value 0 (an exact match on the threshold) must land on the "yes" side. Writing the -1 pair and leaving the rest to the default does this naturally:
SELECT employee_id,
salary,
DECODE(SIGN(salary - 10000),
-1, 'Below 10000',
'At least 10000') AS salary_check
FROM employees;Three-way comparisons
Because SIGN has three outcomes, one DECODE can separate below, equal and above:
SELECT employee_id,
salary,
DECODE(SIGN(salary - 10000),
-1, 'Below 10000',
0, 'Exactly 10000',
1, 'Above 10000') AS salary_position
FROM employees;This version has no default. That is safe here only because the salary is never NULL in the sample table. If a row could produce NULL, the result would be NULL, which is the behavior DECODE has whenever nothing matches and no default is given.
Building bands with nested DECODE
To reproduce a banded searched CASE such as "Low, Medium, High", nest one DECODE inside another. The inner DECODE takes the place of the outer default:
SELECT employee_id,
salary,
DECODE(SIGN(salary - 5000),
-1, 'Low',
DECODE(SIGN(salary - 10000),
-1, 'Medium',
'High')) AS salary_band
FROM employees;Trace a salary of 5000. SIGN(5000 - 5000) is 0, which is not -1, so the outer DECODE returns its default, the inner DECODE. There SIGN(5000 - 10000) is -1, so the result is 'Medium'. A salary of exactly 10000 reaches the inner DECODE with a sign of 0, matches nothing, and gets 'High'.
The searched CASE equivalent is shorter and does not depend on counting parentheses:
SELECT employee_id,
salary,
CASE
WHEN salary < 5000 THEN 'Low'
WHEN salary < 10000 THEN 'Medium'
ELSE 'High'
END AS salary_band
FROM employees;Both return the same band for every non-NULL salary. A third or fourth band in DECODE means another level of nesting, and readability falls quickly.
NULL: the quiet failure
SIGN of NULL is NULL, and the earlier two-way example sends NULL to the default. For a NULL salary, DECODE(SIGN(salary - 10000), 1, 'Above 10000', 'At or below 10000') returns 'At or below 10000'. That label is wrong, because the value is unknown.
A searched CASE has the same blind spot, since salary > 10000 is not true for NULL and execution falls to ELSE. The difference is that DECODE lets you catch the case with a NULL search value, because DECODE treats NULL as equal to NULL:
SELECT employee_id,
commission_pct,
DECODE(SIGN(commission_pct - 0.2),
NULL, 'No commission',
1, 'High commission',
'Low commission') AS commission_level
FROM employees;Put the NULL pair first or anywhere before the default. It matches when SIGN returns NULL, so employees without a commission get their own label.
Dates and other numeric-like values
SIGN works on any numeric expression. Subtracting one DATE from another gives a number of days, so date comparisons work the same way:
SELECT employee_id,
hire_date,
DECODE(SIGN(hire_date - DATE '2005-01-01'),
-1, 'Hired before 2005',
'Hired in 2005 or later') AS hire_group
FROM employees;Strings cannot be used this way. SIGN('B' - 'A') fails with ORA-01722 (invalid number), because Oracle tries to convert the text to a number. To compare strings with DECODE you would need a different trick, and a searched CASE is the better answer.
Gotchas and limits
- Overflow and precision. The subtraction happens before SIGN, so very large or very precise numbers can lose information. Subtracting two values near the limits of NUMBER precision can round to 0.
- Inclusive versus exclusive boundaries. Re-check the value 0 whenever you convert a condition. Many off-by-one errors come from treating
>=as>. - Evaluation of the nested pattern. DECODE evaluates only the arguments it needs, similar to CASE, but you should not rely on that to avoid errors such as division by zero in an unused branch. Test the specific case.
- Maintainability. Each added band deepens the nesting. Teams reading the query later usually prefer the CASE form.
When to use SIGN with DECODE
Use CASE for new code. It is standard SQL, reads naturally for ranges, and can be used in PL/SQL as well as SQL. The SIGN technique is mostly worth knowing for two reasons: you will meet it in older Oracle code, and you will sometimes need to rewrite it. When you convert, work out each branch's condition from the table above, keep the boundary behavior identical, and make the NULL behavior explicit with IS NULL.
Exercise
Rewrite this DECODE as a searched CASE with identical results for non-NULL salaries: DECODE(SIGN(salary - 8000), -1, 'Under', 0, 'At target', 'Over'). Then say what each version returns when salary is NULL.
Show answer
CASE WHEN salary < 8000 THEN 'Under' WHEN salary = 8000 THEN 'At target' ELSE 'Over' END. When salary is NULL, SIGN returns NULL. No search value is NULL, so DECODE returns the default 'Over'. The CASE conditions are both unknown, so it also falls to ELSE and returns 'Over'. Both are misleading, and the fix is to handle NULL first, with NULL, 'Unknown' as a pair in DECODE or WHEN salary IS NULL THEN 'Unknown' in CASE.
Exercise
A query uses DECODE(SIGN(score - 50), 1, 'Pass', 'Fail'). A student scores exactly 50. What is the result, and how would you change the query so that 50 passes?
Show answer
SIGN(0) is 0, which does not match 1, so the default 'Fail' is returned. To make 50 pass, use DECODE(SIGN(score - 50), -1, 'Fail', 'Pass'), so that only values below 50 fail and 0 falls to the 'Pass' default.