Sub topic 11
Result data types and ORA-00932 errors
This chapter assumes you can write simple and searched CASE expressions, you know how DECODE compares values, and you have met the basic Oracle data types NUMBER, VARCHAR2 and DATE. It covers how Oracle decides what data type a conditional expression returns, why a mismatch raises ORA-00932, and how DECODE differs from CASE here.
Why a conditional expression has one data type
A CASE expression is a single column value. A column has one data type, so every branch that can supply that value must fit one type. Oracle fixes that type when it parses the statement, before it reads any rows. It looks at every THEN result and the ELSE result, whether or not a branch will run.
A mismatch in a branch that never runs therefore still fails. This statement raises an error even though the ELSE branch is unreachable:
SELECT CASE WHEN 1 = 1 THEN 'Yes' ELSE 0 END AS answer
FROM dual;The error is:
ORA-00932: inconsistent datatypes: expected CHAR got NUMBERThe message names the type Oracle expected (set by the earlier branches) and the type it found in the offending branch.
The rules for CASE
The rules apply to the result expressions (THEN and ELSE):
- If all results have the same data type, that type is returned.
- If all results are numeric, Oracle picks the numeric type with the highest precedence and converts the others to it. The precedence order is NUMBER, then BINARYFLOAT, then BINARYDOUBLE. Mixing
1and2.5d(a BINARYDOUBLE literal) returns BINARYDOUBLE. - Any other mix raises
ORA-00932. Oracle does not turn a number into a string, or a string into a date, to make CASE results agree. - The literal
NULLis allowed in any branch and takes the type of the other branches.
The rules for the compared values depend on the CASE form:
- In a simple CASE, the expression after
CASEand everyWHENvalue must share a type, or all be numeric.CASE department_id WHEN '10' THEN ...compares a NUMBER with a string and is rejected. - In a searched CASE, each
WHENis an ordinary condition. Normal comparison rules apply, including implicit conversion, soWHEN employee_id = '100'works. Conversion in conditions has its own risks, covered later in this chapter.
Common mismatches and fixes
Number mixed with text
A frequent case is a label column that also needs to show a number:
SELECT employee_id,
CASE
WHEN commission_pct IS NULL THEN 'None'
ELSE commission_pct
END AS commission_text
FROM employees;This fails with ORA-00932: the first result is text and the ELSE result is a NUMBER. Convert the number explicitly:
SELECT employee_id,
CASE
WHEN commission_pct IS NULL THEN 'None'
ELSE TO_CHAR(commission_pct, 'FM0.00')
END AS commission_text
FROM employees;The FM format modifier removes padding spaces. Without it, TO_CHAR can add a leading blank for the sign.
Date mixed with text
The same problem occurs with dates:
SELECT employee_id,
CASE
WHEN hire_date < DATE '2000-01-01' THEN 'Before 2000'
ELSE hire_date
END AS hire_info
FROM employees;This raises ORA-00932: inconsistent datatypes: expected CHAR got DATE. Use TO_CHAR(hire_date, 'YYYY-MM-DD') in the ELSE branch, or return the date and use NULL for the other case:
SELECT employee_id,
CASE
WHEN hire_date >= DATE '2000-01-01' THEN hire_date
ELSE NULL
END AS hired_since_2000
FROM employees;This version returns a DATE in every branch, with NULL standing in where there is no date. It keeps the column a real DATE, so you can sort and filter it correctly later.
Every branch is NULL
Oracle documents that you cannot write the literal NULL as every result and as the ELSE. With no real value, no type can be inferred, and the statement raises ORA-00932. If you need a typed NULL, cast it: CAST(NULL AS VARCHAR2(10)).
Numeric branches: precision and format
Because all-numeric branches are allowed to mix, this works:
SELECT employee_id,
CASE
WHEN salary < 5000 THEN 0
ELSE salary * 1.05
END AS adjusted_raise_base
FROM employees;The return type is NUMBER. The branches differ in scale, but they are still one type. The result is not a mixed column.
When you combine NUMBER with BINARYFLOAT or BINARYDOUBLE, the NUMBER branches are converted to the binary type. Binary floating-point values cannot represent many decimal fractions exactly, so a decimal branch such as 0.1 can come back as 0.10000000000000001 after the conversion. Keep money columns as NUMBER in every branch to avoid it.
DECODE has different rules
DECODE converts instead of rejecting. Oracle documents these behaviors:
- The return type is the type of the first result argument. If the first result is CHAR, or is NULL, the return type is VARCHAR2.
- Oracle converts the expression and each search value to the type of the first search value before it compares them.
- Later results are implicitly converted to the type of the first result.
So the statement that failed with CASE succeeds with DECODE:
SELECT DECODE(1, 1, 'Yes', 0) AS answer
FROM dual;The 0 is converted to '0', and the query returns Yes. The conversion can fail at runtime if a value cannot be converted:
SELECT DECODE(2, 1, 10, 'abc') AS answer
FROM dual;The first result is a NUMBER. Search value 2 matches nothing, so the default 'abc' is returned and Oracle tries to convert it to a number. The statement raises ORA-01722: invalid number.
A comparison:
| Behavior | CASE | DECODE |
|---|---|---|
| Return type | Common type of all results | Type of the first result |
| Mixed NUMBER and text results | `ORA-00932` at parse time | Later results converted to the first result's type |
| Unconvertible value | Rejected before any row is read | `ORA-01722` (or similar) when the value is converted |
| Result of an unmatched value with no default | `NULL` | `NULL` |
Neither behavior is a feature to rely on. DECODE's silent conversion hides type mistakes until the data triggers them, and the failure may show up only for certain rows. CASE fails immediately and on every run. When you rewrite DECODE as CASE, expect ORA-00932 to expose mixed-type results that DECODE had been converting quietly.
Implicit conversion in WHEN conditions
A searched CASE condition like WHEN job_code = 10, where job_code is a VARCHAR2 column, makes Oracle convert the column to a number. This causes two problems:
- A single row with non-numeric text in the column raises
ORA-01722: invalid number. It can appear only after new data is loaded. - Converting the column side can stop an index on that column from being used.
Compare like with like instead: WHEN job_code = '10'. The same advice applies to dates. Use DATE '2000-01-01' or TO_DATE(...) with an explicit format rather than a bare string.
Checking the type you got
To see which type a CASE returned, wrap it in DUMP or create a view and describe it:
CREATE OR REPLACE VIEW salary_band_v AS
SELECT employee_id,
CASE
WHEN salary < 5000 THEN 'Low'
ELSE 'High'
END AS salary_band
FROM employees;
DESCRIBE salary_band_vDESCRIBE shows the column as VARCHAR2 with the length Oracle derived from the longest literal. If the length matters (for example when you insert the result into a table column), add CAST(... AS VARCHAR2(20)) to set it yourself.
Best practices
- Make every branch return the same type, and convert with
TO_CHAR,TO_NUMBERorTO_DATEplus an explicit format. - Use
NULLrather than a placeholder string when a numeric or date column has "no value" cases. Placeholders force a text type on the whole column. - Quote string literals when you compare them to character columns, and write numbers without quotes when you compare to number columns.
- Use
CAST(NULL AS type)when you need a typed NULL. - Read an
ORA-00932message as "expected X got Y": Y is the branch to fix.
Exercise
The query SELECT CASE WHEN salary > 10000 THEN 'High' ELSE salary END FROM employees raises ORA-00932. Explain why, and write a corrected version that keeps the numeric salary readable as text.
Show answer
The THEN result is a string and the ELSE result is a NUMBER. CASE does not convert between them, so Oracle rejects the mix when it parses the statement. A fix is to convert the number: SELECT CASE WHEN salary > 10000 THEN 'High' ELSE TO_CHAR(salary) END FROM employees. Both branches now return VARCHAR2.
Exercise
SELECT DECODE(3, 1, 100, 'none') FROM dual fails with ORA-01722, but SELECT DECODE(3, 1, 'one', 100) FROM dual returns 100. Why do the two statements behave differently?
Show answer
DECODE returns the type of its first result. In the first statement the first result is the number 100, so the default 'none' must be converted to a number, which fails with ORA-01722. In the second statement the first result is the string 'one', so the default 100 is converted to the string '100' and returned.