Error
Error Code:
ORA-30071
Oracle ORA-30071: Datetime Conversion Error
Description
The ORA-30071 error indicates a failure during the conversion between a datetime/interval data type and a string. This often happens because the provided buffer is insufficient or the format string used is invalid, leading to conversion issues.
Error Message
ORA-30071: conversion between datetime/interval and string fail
Known Causes
3 known causesInsufficient Buffer Size
The buffer allocated to store the converted string is too small to accommodate the complete datetime or interval value. This leads to truncation or conversion failure.
Invalid Format String
The format string used in the conversion function (e.g., TO_CHAR, TO_DATE) is incorrect or contains unsupported format elements. This causes the conversion to fail.
Data Type Mismatch
There is an implicit or explicit mismatch between the data type of the datetime/interval value and the format string being used. This can result in conversion errors.
Solutions
3 solutions available1. Explicitly Specify Date Format in TO_DATE and TO_CHAR easy
Ensure consistent date format parsing and conversion by always specifying the format mask.
1
When converting a string to a date using `TO_DATE`, always provide the explicit format mask that matches the string's structure. This is the most common cause of ORA-30071.
SELECT TO_DATE('2023-10-27 10:30:00', 'YYYY-MM-DD HH24:MI:SS') FROM dual;
-- Incorrect example causing ORA-30071:
-- SELECT TO_DATE('27/10/2023', 'YYYY-MM-DD') FROM dual; -- Will fail if format doesn't match
2
Similarly, when converting a date to a string using `TO_CHAR`, specify the desired format mask.
SELECT TO_CHAR(SYSDATE, 'YYYY-MM-DD HH24:MI:SS') FROM dual;
-- SELECT TO_CHAR(SYSDATE, 'DD-MON-RR') FROM dual;
3
If you are encountering this error in application code (e.g., Java, Python), ensure that the date formats used in your SQL statements or ORM configurations precisely match the date strings being passed.
-- Example in Java (JDBC):
// PreparedStatement ps = connection.prepareStatement("INSERT INTO my_table (my_date_column) VALUES (TO_DATE(?, 'YYYY-MM-DD HH24:MI:SS'))");
// ps.setString(1, '2023-10-27 10:30:00');
2. Verify NLS_DATE_FORMAT Parameter medium
Check and adjust the NLS_DATE_FORMAT session parameter if implicit conversions are intended.
1
Check the current `NLS_DATE_FORMAT` setting for your session.
SELECT value FROM NLS_SESSION_PARAMETERS WHERE parameter = 'NLS_DATE_FORMAT';
2
If you rely on implicit date conversions (which is generally discouraged for production systems), ensure that the string you are trying to convert matches the `NLS_DATE_FORMAT`. If it doesn't, you'll get ORA-30071.
-- If NLS_DATE_FORMAT is 'DD-MON-RR' and you try:
-- SELECT TO_DATE('2023-10-27', 'YYYY-MM-DD') FROM dual; -- This will likely fail.
-- Instead, ensure your string matches or use explicit TO_DATE with format mask.
3
To temporarily change the `NLS_DATE_FORMAT` for your session (use with caution):
ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD HH24:MI:SS';
-- Then you might be able to use implicit conversions, but explicit is better.
4
For permanent changes, you would modify the `NLS_DATE_FORMAT` parameter in `init.ora` or `spfile`, but this affects the entire database and should be carefully considered.
-- Example for spfile (requires restart):
-- ALTER SYSTEM SET NLS_DATE_FORMAT = 'YYYY-MM-DD HH24:MI:SS' SCOPE=SPFILE;
3. Handle Invalid or Null Date Values easy
Use NULLIF or COALESCE to gracefully handle strings that cannot be converted to dates.
1
When a string might be invalid or empty, use `NULLIF` to return NULL instead of raising an error.
SELECT TO_DATE(NULLIF(your_string_column, 'INVALID_OR_EMPTY_VALUE'), 'YYYY-MM-DD') FROM your_table;
-- If 'your_string_column' contains 'INVALID_OR_EMPTY_VALUE', TO_DATE will receive NULL and return NULL.
2
Alternatively, use `COALESCE` to provide a default date value if the conversion fails or the input is NULL.
SELECT COALESCE(TO_DATE(your_string_column, 'YYYY-MM-DD'), SYSDATE) FROM your_table;
-- If TO_DATE fails or your_string_column is NULL, SYSDATE will be used.
3
Consider adding a `WHERE` clause to filter out problematic rows before attempting conversion if possible.
SELECT TO_DATE(your_string_column, 'YYYY-MM-DD') FROM your_table WHERE your_string_column IS NOT NULL AND REGEXP_LIKE(your_string_column, '^\d{4}-\d{2}-\d{2}$');