Sub topic 10
Understanding conversion errors
This chapter assumes you can run SQL in SQL*Plus or SQL Developer. It also assumes you have met format models, TO_CHAR, TO_DATE, and TO_NUMBER in earlier chapters. Here you will learn to read the errors these functions raise, find the cause, and decide whether to fix the data, the format model, or the query.
Where conversion errors come from
A conversion error means Oracle was given a value and a rule (the format model, or a default rule when none is given) that do not agree. Three situations cause nearly all of them:
- The text contains characters the target type cannot hold, such as letters in a number.
- The format model and the text differ in structure, such as a different separator, a missing part, or extra trailing text.
- The text matches the model structurally but is not a real value, such as month 13 or day 32.
The error number usually tells you which situation you are in. Oracle reports the first problem it finds and stops, so one fix can reveal the next error.
ORA-01722: invalid number
ORA-01722 is the error you will meet most often. It means Oracle tried to turn text into a number and could not.
-- Each statement raises ORA-01722: invalid number
SELECT TO_NUMBER('12x') FROM dual;
SELECT TO_NUMBER('1,234.56') FROM dual;
SELECT TO_NUMBER('$99.50') FROM dual;The second and third statements surprise people. Without a format model, TO_NUMBER accepts only an optional sign, digits, and one decimal separator. Group separators and currency symbols need a model:
SELECT TO_NUMBER('1,234.56', '9,999.99') AS n1,
TO_NUMBER('$99.50', '$99.99') AS n2
FROM dual;The hidden source: implicit conversion
Often your query contains no TO_NUMBER at all. When you compare a VARCHAR2 column with a number, Oracle converts the column to a number for every row it examines. A single non-numeric value is enough to raise ORA-01722.
-- Raises ORA-01722 if any row of order_ref holds a non-numeric value
SELECT * FROM orders WHERE order_ref = 1001;
-- Compare text with text
SELECT * FROM orders WHERE order_ref = '1001';This kind of failure can look random. The query may run for weeks, then fail after someone inserts the value 'N/A'. The optimizer can also evaluate predicates in an order you did not expect, so a query that worked yesterday may fail today because a different execution plan reached the bad row first. Do not rely on one WHERE condition to filter out bad rows before another condition converts them. Make the conversion itself safe, as described later in this chapter.
ORA-01722 in PL/SQL
Inside a SQL statement the error is ORA-01722, and PL/SQL maps it to the INVALID_NUMBER exception. A procedural assignment such as v_num := 'abc'; raises VALUE_ERROR (ORA-06502: character to number conversion error). The cause is the same, but the exception name differs, so an exception handler written for one will not catch the other.
Date errors: reading the code
Date conversions have a larger family of errors. The code tells you which part of the match failed.
| Error | Message | Typical cause |
|---|---|---|
| ORA-01843 | not a valid month | Month number above 12, or a month name in the wrong language |
| ORA-01861 | literal does not match format string | Text and model differ in structure, usually under `FX`, or an implicit conversion |
| ORA-01830 | date format picture ends before converting entire input string | Text has more characters than the model consumes |
| ORA-01840 | input value not long enough for date format | Text ends before the model does |
| ORA-01858 | a non-numeric character was found where a numeric was expected | Letters where the model expects digits |
| ORA-01847 | day of month must be between 1 and last day of month | Day 0 or day above 31 |
| ORA-01839 | date not valid for month specified | A day that does not exist in that month, such as 31 February |
ORA-01843: month problems
The simplest case is a month number outside 1 to 12:
-- ORA-01843: not a valid month
SELECT TO_DATE('15-13-2026', 'DD-MM-YYYY') FROM dual;The harder case happens when the text is valid but the language is wrong. With MON or MONTH in the model, Oracle reads the month name in the session's NLS_DATE_LANGUAGE. A session set to French does not recognize JAN the way an English session does, so the same code can pass in one session and fail in another. Fix it by naming the language in the third argument:
SELECT TO_DATE('15-JAN-2026', 'DD-MON-YYYY', 'NLS_DATE_LANGUAGE=ENGLISH') AS parsed
FROM dual;ORA-01861 and the effect of FX
By default, TO_DATE is forgiving. Punctuation in the text matches any punctuation in the model, and a number may lose its leading zeros. These calls all succeed:
SELECT TO_DATE('2026/11/28', 'YYYY-MM-DD') AS slash_ok,
TO_DATE('2026-1-5', 'YYYY-MM-DD') AS short_ok
FROM dual;The FX (format exact) modifier turns that leniency off. The text must then match the model character for character, including separators and digit counts:
-- Both raise ORA-01861: literal does not match format string
SELECT TO_DATE('2026/11/28', 'FXYYYY-MM-DD') FROM dual;
SELECT TO_DATE('2026-1-5', 'FXYYYY-MM-DD') FROM dual;ORA-01861 also appears when you compare a DATE column with a string and let Oracle convert the string. Oracle then uses the session's NLS_DATE_FORMAT. If that is DD-MON-RR and your string is '2026-11-28', the conversion fails with a date error such as ORA-01861 or ORA-01843. The same query works in a session with a different default. Use TO_DATE with an explicit model or a DATE '2026-11-28' literal instead.
ORA-01830 and ORA-01840: length mismatches
These two errors are mirror images. ORA-01830 means the model finished but text remained. The usual cause is a time portion that the model does not describe:
-- ORA-01830: date format picture ends before converting entire input string
SELECT TO_DATE('28-11-2026 14:30', 'DD-MM-YYYY') FROM dual;
-- Fix: describe the time part
SELECT TO_DATE('28-11-2026 14:30', 'DD-MM-YYYY HH24:MI') FROM dual;Another cause is YY against a four-digit year. YY consumes two digits, which leaves 26 unread:
-- ORA-01830
SELECT TO_DATE('28-11-2026', 'DD-MM-YY') FROM dual;ORA-01840 is the reverse. The text runs out before the model does:
-- ORA-01840: input value not long enough for date format
SELECT TO_DATE('2026-11', 'YYYY-MM-DD') FROM dual;Values that look right but are not
If the structure matches but the value is impossible, you get a range error:
-- ORA-01847: day of month must be between 1 and last day of month
SELECT TO_DATE('32-01-2026', 'DD-MM-YYYY') FROM dual;
-- ORA-01839: date not valid for month specified
SELECT TO_DATE('31-02-2026', 'DD-MM-YYYY') FROM dual;Errors in the format model itself
Sometimes the model is wrong rather than the data. A model that Oracle cannot parse raises an error such as ORA-01481 (invalid number format model) for number models, or ORA-01821 (date format not recognized) for date models. These errors fail on every row, so you see them the first time you run the statement.
-- ORA-01481: invalid number format model
SELECT TO_NUMBER('12', '9X9') FROM dual;Mistakes that raise no error
The most damaging conversion bugs return a value with no error at all.
- A too-narrow number model.
TO_CHARcannot fit the value, so it returns a row of hash characters instead of failing. MMwhereMIwas meant.HH24:MM:SSis valid, becauseMMis the month. It prints the month number in the minutes position.
SELECT TO_CHAR(12345, '9999') AS too_narrow,
TO_CHAR(DATE '2026-11-28', 'HH24:MM:SS') AS wrong_minutes
FROM dual;The first column returns #####. The second returns 00:11:00, which looks like a time but shows the month 11 where the minutes should be. Check output for these cases, because the database will not warn you.
Finding the rows that cause the error
When a query fails because of one bad value among millions, find the value instead of guessing. In Oracle 12.2 and later, VALIDATE_CONVERSION returns 1 for a convertible value and 0 for a bad one:
SELECT order_ref
FROM orders
WHERE order_ref IS NOT NULL
AND VALIDATE_CONVERSION(order_ref AS NUMBER) = 0;For dates, pass the model the same way you would to TO_DATE:
SELECT hire_text
FROM staging_hires
WHERE hire_text IS NOT NULL
AND VALIDATE_CONVERSION(hire_text AS DATE, 'DD-MM-YYYY') = 0;The IS NOT NULL test is there because VALIDATE_CONVERSION returns 1 for a NULL input, so a NULL is never reported as bad. Once you have the list, decide whether to correct the source data, widen the format model, or accept the bad values and map them to a fallback.
To keep a query running despite bad values, use the fallback clause or a guard:
SELECT TO_NUMBER(order_ref DEFAULT -1 ON CONVERSION ERROR) AS order_num
FROM orders;
SELECT CASE WHEN VALIDATE_CONVERSION(order_ref AS NUMBER) = 1
THEN TO_NUMBER(order_ref)
END AS order_num
FROM orders;Both versions return a value for every row, so no error stops the query. The CASE form returns NULL for bad values, and the DEFAULT form returns the fallback you chose. Pick a fallback that cannot be mistaken for real data. A value like -1 may be a legal order number in another table.
Summary of habits
- Treat ORA-01722 as "the text is not a number under these rules", and check for implicit conversions when no
TO_NUMBERappears. - Treat ORA-01830 and ORA-01840 as length mismatches, ORA-01843 as a month or language problem, and ORA-01861 as a structure mismatch.
- Use
FXwhen you want strict input checking, and leave it off when you want lenient parsing. - Set the language and separators explicitly with the
nlsparamargument. - Inspect output for
#####and for suspicious time values. - Use
VALIDATE_CONVERSIONto find bad rows andDEFAULT ... ON CONVERSION ERRORto survive them.
Exercise
The statement SELECT TO_DATE('2026-11-28 09:15', 'YYYY-MM-DD') FROM dual; fails. Which error does it raise, why, and how do you fix it?
Show answer
It raises ORA-01830 (date format picture ends before converting entire input string). The model describes only the date, but the text also contains a time, so characters are left over after the model finishes. Add the time to the model: TO_DATE('2026-11-28 09:15', 'YYYY-MM-DD HH24:MI').
Exercise
A query SELECT * FROM payments WHERE refcode = 5500; ran for months and now fails with ORA-01722. The column refcode is VARCHAR2 and there is no TO_NUMBER in the query. Explain the cause, then write a fixed query and a query that lists the offending rows.
Show answer
Oracle implicitly converts refcode to a number for each row it examines, because it is compared with a number. A newly inserted non-numeric value makes that conversion fail. Fixed query: SELECT * FROM payments WHERE refcode = '5500'; Offending rows: SELECT refcode FROM payments WHERE refcode IS NOT NULL AND VALIDATECONVERSION(refcode AS NUMBER) = 0;
Exercise
What does TODATE('2026/11/28', 'FXYYYY-MM-DD') return, and what does TODATE('2026/11/28', 'YYYY-MM-DD') return?
Show answer
The first raises ORA-01861 because FX requires the text to match the model exactly, including the separators. The second succeeds and returns 28 November 2026, because without FX any punctuation in the text matches the punctuation in the model.