Sub topic 11
Result Data Types and ORA-00932 Errors
This chapter assumes you know basic SELECT statements, Oracle data types such as NUMBER, VARCHAR2, and DATE, and the difference between a SQL NULL and a value.
A CASE expression returns one result from its THEN and ELSE branches. Oracle must determine one compatible data type for the whole expression before it runs the query. The result branches cannot arbitrarily return different types.
How Oracle determines the result type
For a CASE expression, inspect every possible result:
SELECT CASE
WHEN 1 = 1 THEN 100
ELSE 0
END AS result_number
FROM dual;Both branches return numbers, so Oracle returns a numeric result. Numeric types such as NUMBER, BINARY_FLOAT, and BINARY_DOUBLE follow Oracle's numeric precedence rules.
Character results should also use compatible character types:
SELECT CASE
WHEN 1 = 1 THEN 'Open'
ELSE 'Closed'
END AS status
FROM dual;The following table shows common safe and unsafe combinations.
| Branch results | Result |
|---|---|
| `NUMBER` and `NUMBER` | Numeric result |
| `VARCHAR2` and `VARCHAR2` | Character result |
| `DATE` and `DATE` | Date result |
| `NUMBER` and character text such as `'Unknown'` | Usually `ORA-00932` |
| `DATE` and character text such as `'None'` | Usually `ORA-00932` |
A common mistake is to mix a value with a display label:
-- This raises ORA-00932: inconsistent datatypes.
-- SELECT CASE
-- WHEN 1 = 1 THEN 100
-- ELSE 'Unknown'
-- END
-- FROM dual;The expression tries to return either a number or text. Decide what the expression represents, then make every branch return that type.
If the result is intended for display, convert the number to text:
SELECT CASE
WHEN 1 = 1 THEN TO_CHAR(100)
ELSE 'Unknown'
END AS displayed_value
FROM dual;If the result must remain numeric, use a numeric fallback instead:
SELECT CASE
WHEN 1 = 1 THEN 100
ELSE CAST(NULL AS NUMBER)
END AS numeric_value
FROM dual;Making `NULL` have the intended type
An untyped NULL often works because Oracle can infer its type from the other branch:
SELECT CASE
WHEN 1 = 1 THEN 100
ELSE NULL
END AS amount
FROM dual;For clarity, especially in larger expressions or views, explicitly cast it:
SELECT CASE
WHEN 1 = 1 THEN DATE '2026-01-01'
ELSE CAST(NULL AS DATE)
END AS effective_date
FROM dual;The same technique is useful in conditional aggregation. Keep the ELSE value numeric when the aggregate is numeric:
SELECT SUM(
CASE
WHEN 1 = 1 THEN 25
ELSE 0
END
) AS total_amount
FROM dual;Do not use '0' as the fallback for a numeric SUM. A text zero can force implicit conversion or produce a datatype error.
A practical debugging method
When Oracle reports ORA-00932: inconsistent datatypes, check the expression in this order:
- List the data type of every
THENresult. - Check the
ELSEresult, including an implicit or untypedNULL. - Check nested functions such as
NVL,COALESCE, and aggregates. - Add an explicit conversion to the type the expression should return.
- Test the corrected expression separately before placing it in a larger query.
The branches converge into one result type before the selected value is returned.
Exercise
How should you write a CASE expression that returns either a salary number or no value?
Show answer
Keep both branches numeric, for example: CASE WHEN salary > 10000 THEN salary ELSE CAST(NULL AS NUMBER) END.