Lab-verifiedReproduced and fixed on Oracle AI Database 26ai Free 23.26.3.0.0 · 2026-11-29 · container image gvenzl/oracle-free:23-slim
An Oracle DATE has years from -4713 to 9999, and there is no year 0. ORA-01841 appears when TO_DATE reads a year of 0, or when date arithmetic goes past 9999-12-31.
The tricky part is that one bad row stops the whole statement, and a wrong format mask can turn good-looking data into year 0. All three cases below were run on Oracle 26ai Free.
01Year 0000 in text data converted with TO_DATE
A staging table holds dates as text. One row, A-002, has 0000-01-01. INSERT INTO orders SELECT …, TO_DATE(order_dt, 'YYYY-MM-DD') fails with ORA-01841 and inserts nothing.
WHERE VALIDATE_CONVERSION(order_dt AS DATE, 'YYYY-MM-DD') = 0 found exactly that row. Using TO_DATE(order_dt DEFAULT NULL ON CONVERSION ERROR, 'YYYY-MM-DD') inserted all 3 rows, with A-002 stored as NULL.
Setup
CREATE TABLE stg_orders (order_no VARCHAR2(10), order_dt VARCHAR2(10));
INSERT INTO stg_orders VALUES ('A-001', '2024-11-30');
INSERT INTO stg_orders VALUES ('A-002', '0000-01-01');
INSERT INTO stg_orders VALUES ('A-003', '2024-12-02');
COMMIT;
CREATE TABLE orders (order_no VARCHAR2(10), order_dt DATE);Table created.
1 row created.
1 row created.
1 row created.
Commit complete.
Table created.INSERT INTO orders
SELECT order_no, TO_DATE(order_dt, 'YYYY-MM-DD') FROM stg_orders;SELECT order_no, TO_DATE(order_dt, 'YYYY-MM-DD') FROM stg_orders
*
ERROR at line 2:
ORA-01841: (full) year must be between -4713 and +9999, and not be 0
Help: https://docs.oracle.com/error-help/db/ora-01841/SELECT order_no, order_dt FROM stg_orders
WHERE VALIDATE_CONVERSION(order_dt AS DATE, 'YYYY-MM-DD') = 0;ORDER_NO ORDER_DT
---------- ----------
A-002 0000-01-01
1 row selected.DEFAULT NULL ON CONVERSION ERROR hides bad values. Check them with VALIDATE_CONVERSION first and decide what they should be.
INSERT INTO orders
SELECT order_no, TO_DATE(order_dt DEFAULT NULL ON CONVERSION ERROR, 'YYYY-MM-DD') FROM stg_orders;
SELECT order_no, TO_CHAR(order_dt, 'YYYY-MM-DD') order_dt FROM orders ORDER BY order_no;3 rows created.
ORDER_NO ORDER_DT
---------- ----------
A-001 2024-11-30
A-002
A-003 2024-12-02
3 rows selected.02Date arithmetic past 9999-12-31
Contracts with no end date are often stored as 9999-12-31. SELECT end_date + 1 then tries to produce year 10000 and the query fails with ORA-01841.
Filtering on end_date = DATE '9999-12-31' showed contract 2. Skipping the +1 for that value with CASE WHEN end_date = DATE '9999-12-31' THEN end_date ELSE end_date + 1 END returned 2025-04-01 and 9999-12-31.
Setup
CREATE TABLE contracts (contract_id NUMBER, end_date DATE);
INSERT INTO contracts VALUES (1, DATE '2025-03-31');
INSERT INTO contracts VALUES (2, DATE '9999-12-31');
COMMIT;Table created.
1 row created.
1 row created.
Commit complete.SELECT contract_id, end_date + 1 AS next_start FROM contracts;ERROR:
ORA-01841: (full) year must be between -4713 and +9999, and not be 0
Help: https://docs.oracle.com/error-help/db/ora-01841/
no rows selectedSELECT contract_id, TO_CHAR(end_date, 'YYYY-MM-DD') end_date FROM contracts
WHERE end_date = DATE '9999-12-31';CONTRACT_ID END_DATE
----------- ----------
2 9999-12-31
1 row selected.SELECT contract_id,
TO_CHAR(CASE WHEN end_date = DATE '9999-12-31' THEN end_date ELSE end_date + 1 END, 'YYYY-MM-DD') AS next_start
FROM contracts ORDER BY contract_id;CONTRACT_ID NEXT_START
----------- ----------
1 2025-04-01
2 9999-12-31
2 rows selected.03Two-digit years read with a YYYY format mask
Legacy data has 99/12/31 and 00/01/15. With TO_DATE(doc_dt, 'YYYY/MM/DD'), 00 becomes year 0 and the query fails with ORA-01841.
The other row does not fail, but it is wrong: 99/12/31 became 0099-12-31. Using the RR mask, TO_DATE(doc_dt, 'RR/MM/DD'), returned 1999-12-31 and 2000-01-15.
Setup
CREATE TABLE legacy_in (doc_no VARCHAR2(10), doc_dt VARCHAR2(8));
INSERT INTO legacy_in VALUES ('D-1', '99/12/31');
INSERT INTO legacy_in VALUES ('D-2', '00/01/15');
COMMIT;Table created.
1 row created.
1 row created.
Commit complete.SELECT doc_no, TO_DATE(doc_dt, 'YYYY/MM/DD') doc_dt FROM legacy_in;ERROR:
ORA-01841: (full) year must be between -4713 and +9999, and not be 0
Help: https://docs.oracle.com/error-help/db/ora-01841/
no rows selectedSELECT doc_no, TO_CHAR(TO_DATE(doc_dt, 'YYYY/MM/DD'), 'YYYY-MM-DD') doc_dt FROM legacy_in WHERE doc_no = 'D-1';DOC_NO DOC_DT
---------- ----------
D-1 0099-12-31
1 row selected.Only the 00 row raised an error. Rows like 99 silently became year 0099, so check the whole column after fixing the mask.
SELECT doc_no, TO_CHAR(TO_DATE(doc_dt, 'RR/MM/DD'), 'YYYY-MM-DD') doc_dt FROM legacy_in ORDER BY doc_no;DOC_NO DOC_DT
---------- ----------
D-1 1999-12-31
D-2 2000-01-15
2 rows selected.FAQ
How do I find which row causes ORA-01841?
Use VALIDATE_CONVERSION(col AS DATE, 'format') = 0 in the WHERE clause. It returns 0 for values that cannot be converted, without raising an error.
Why does 9999-12-31 + 1 give ORA-01841?
The result would be year 10000, and DATE stops at 9999. Exclude the 9999-12-31 value with CASE before adding days.
Should I use YYYY or RR for 2-digit years?
RR. On this page YYYY turned 00 into year 0 (error) and 99 into 0099, while RR gave 2000 and 1999.
Can TO_DATE skip invalid dates instead of failing?
Yes, TO_DATE(col DEFAULT NULL ON CONVERSION ERROR, 'format') returns NULL for values it cannot convert.
How we verify
Each scenario starts from fresh database users. A script runs the setup, confirms the exact error code, applies the fix and runs the statement again. The output shown is what the database printed, saved by the script.
lab_a and lab_b are ordinary users created for the test; system is the DBA account. Oracle AI Database 26ai Free Release 23.26.3.0.0 - Develop, Learn, and Run for Free 23.26.3.0.0