Sub topic 10
Understanding Conversion Errors
This chapter assumes that you know basic SELECT and WHERE syntax and understand the NUMBER, VARCHAR2, and DATE data types. Oracle raises conversion errors when it cannot interpret a value as the data type required by an expression.
How conversion errors happen
A conversion can be explicit or implicit:
- An explicit conversion calls a function such as
TO_NUMBER,TO_DATE, orTO_CHAR. - An implicit conversion happens automatically when Oracle needs compatible data types.
Explicit conversion is safer because you control the format model. An implicit conversion can depend on session settings such as NLS_DATE_FORMAT and NLS_NUMERIC_CHARACTERS.
For example, suppose order_ref is a VARCHAR2 column:
-- Oracle may try to convert every order_ref value to NUMBER.
SELECT *
FROM orders
WHERE order_ref = 1001;
-- Compare text with text instead.
SELECT *
FROM orders
WHERE order_ref = '1001';If any row contains a value such as 'A1001', the first query can fail with ORA-01722: invalid number. Oracle may also avoid a normal index on order_ref because it must convert the column values.
Common Oracle errors
| Error | Typical cause | Safer approach |
|---|---|---|
| ORA-01722: invalid number | Text contains nonnumeric characters | Validate or clean the text before `TO_NUMBER` |
| ORA-01843: not a valid month | Month text or number does not match the expected date format | Use a matching format model and language |
| ORA-01861: literal does not match format string | Input text and format model have different elements | Match each input element explicitly |
ORA-01722: invalid number
TO_NUMBER fails when the input is not a valid number for the format model.
SELECT TO_NUMBER('12x', '999') AS amount
FROM dual;This raises ORA-01722 because x is not a digit. Commas, decimal marks, currency symbols, and spaces can also cause errors when they do not match the format model or session settings.
ORA-01843: not a valid month
A date format model must agree with the input. This query expects a three-letter month name:
SELECT TO_DATE('15-XYZ-2026', 'DD-MON-YYYY') AS parsed_date
FROM dual;XYZ is not a valid month, so Oracle raises ORA-01843. A language mismatch can cause the same error. For example, JAN might not be understood when the session expects another language.
ORA-01861: literal does not match format string
Every important part of the input should have a corresponding format element:
SELECT TO_DATE('2026/11/28', 'DD-MM-YYYY') AS parsed_date
FROM dual;The input uses year/month/day with slashes, but the model expects day/month/year with hyphens. Oracle cannot match the literal to the model and raises ORA-01861.
A method for diagnosing failures
When a conversion fails, inspect these parts in order:
- Check the actual input value, including spaces and punctuation.
- Confirm the source data type.
- Compare the input with the format model element by element.
- Check session settings such as
NLS_DATE_LANGUAGEandNLS_NUMERIC_CHARACTERS. - Look for implicit conversion caused by comparing unlike data types.
The conversion path can be visualized as follows:
Handling questionable data
Oracle Database 12.2 and later support a fallback for conversion errors:
SELECT TO_NUMBER('12x' DEFAULT -1 ON CONVERSION ERROR) AS amount,
VALIDATE_CONVERSION('12x' AS NUMBER) AS is_valid
FROM dual;The first expression returns -1. VALIDATE_CONVERSION returns 0 for invalid input and 1 for valid input. These tools help you find bad rows without stopping the entire query.
Exercise
Exercise
Why can comparing a VARCHAR2 column named orderref with the number 1001 raise ORA-01722, and how should you write the comparison when orderref stores text?
Show answer
Oracle may convert every order_ref value to NUMBER, so a nonnumeric row can fail. Compare text with text by using the string literal '1001'.