Sub topic 11
Handling Bad Input Safely in Oracle 12.2 and Later
This chapter assumes you know basic SELECT syntax and the NUMBER, VARCHAR2, and DATE data types. It also assumes you can run SQL in an Oracle client.
Conversion functions are useful at system boundaries. For example, an application may send a date as text, or a legacy table may store numbers in a VARCHAR2 column. The input may be invalid, incomplete, or formatted differently from what you expect. A conversion function can then raise an error while processing a query.
Why conversions fail
Oracle raises an error when the input cannot match the requested data type or format model.
| Error | Typical cause |
|---|---|
| ORA-01722 | Text cannot be converted to a number |
| ORA-01843 | A date contains an invalid month |
| ORA-01861 | Input does not match the format model |
For example, abc cannot become a number, and 31-02-2026 is not a valid date.
SELECT TO_NUMBER('abc') AS converted_number
FROM dual;This query raises ORA-01722. One invalid value can cause the whole SQL statement to fail. This is especially important when converting values from a table, because you may not know which row contains the bad input.
Use DEFAULT ON CONVERSION ERROR
Oracle 12.2 introduced DEFAULT ... ON CONVERSION ERROR for conversion functions such as TO_NUMBER and TO_DATE. The clause supplies a fallback value when conversion fails.
SELECT TO_NUMBER('abc' DEFAULT -1 ON CONVERSION ERROR) AS number_value,
TO_DATE(
'31-02-2026' DEFAULT NULL ON CONVERSION ERROR,
'DD-MM-YYYY'
) AS date_value
FROM dual;The first expression returns -1. The second returns NULL. The fallback value must itself be valid for the target data type. For example, a text fallback for TO_NUMBER must also be convertible to a number.
This feature handles errors while converting the input text. It does not hide errors caused by evaluating the input expression itself.
You can combine the clause with a format model and an NLS parameter:
SELECT TO_DATE(
'15-JAN-2026' DEFAULT NULL ON CONVERSION ERROR,
'DD-MON-YYYY',
'NLS_DATE_LANGUAGE=ENGLISH'
) AS parsed_date
FROM dual;The language setting makes the conversion independent of the session's date-language setting.
Test values with VALIDATE_CONVERSION
VALIDATE_CONVERSION checks whether an expression can be converted to a requested data type. It returns 1 for a valid value and 0 for an invalid value.
SELECT VALIDATE_CONVERSION('123.45' AS NUMBER) AS valid_number,
VALIDATE_CONVERSION('12x' AS NUMBER) AS invalid_number
FROM dual;This returns 1 and 0. You can use the result to classify incoming data before converting it.
SELECT CASE
WHEN VALIDATE_CONVERSION('12x' AS NUMBER) = 1
THEN TO_NUMBER('12x')
ELSE NULL
END AS safe_number
FROM dual;For simple conversions, DEFAULT ON CONVERSION ERROR is shorter. VALIDATE_CONVERSION is useful when you need to report, count, or separately process invalid rows.
A safe cleanup pattern
Suppose a staging table stores an amount as text. Convert valid values and retain invalid rows for review.
SELECT amount_text,
TO_NUMBER(
amount_text DEFAULT NULL ON CONVERSION ERROR,
'FM999999990D00',
'NLS_NUMERIC_CHARACTERS=''.,'''
) AS amount_number,
VALIDATE_CONVERSION(
amount_text AS NUMBER,
'NLS_NUMERIC_CHARACTERS=''.,'''
) AS is_valid
FROM import_rows;The exact format model should match the input contract. Do not use a fallback to silently accept values that violate a business rule.
Keep filters safe and index-friendly
A conversion in a WHERE clause can fail, and applying a function to a table column can prevent Oracle from using a normal index on that column. Convert constants instead and leave the indexed column unchanged.
SELECT *
FROM employees
WHERE hire_date >= TO_DATE('2026-01-01', 'YYYY-MM-DD')
AND hire_date < TO_DATE('2027-01-01', 'YYYY-MM-DD');The half-open range includes every time on the first day of 2026 and excludes every time on the first day of 2027.
Exercise
Which expression returns NULL instead of raising an error when the text is not a valid number?
Show answer
TONUMBER(textvalue DEFAULT NULL ON CONVERSION ERROR)