Lab-verifiedReproduced and fixed on Oracle AI Database 26ai Free 23.26.3.0.0 · 2026-11-28 · container image gvenzl/oracle-free:23-slim
ORA-01847 is raised when the day in a date string can't be a day in any month: 00, 32 and so on. (If the day is in 1-31 but the month is too short, like April 31, you get ORA-01839 instead.)
Common sources are legacy data that stores '00' for an unknown day, "next day" logic that adds 1 to the day digits of a string, and source files with typos like '2026-12-32'. All three were reproduced and fixed below.
01'00' stored as the day when the exact day is unknown
members.birth_ymd VARCHAR2(8) had '19900512' and '19850300'. TO_DATE(birth_ymd, 'YYYYMMDD') failed with ORA-01847.
WHERE SUBSTR(birth_ymd, 7, 2) = '00' found member 2. Replacing '00' with '01' in a CASE expression returned both rows: 1990-05-12 and 1985-03-01.
Setup
CREATE TABLE members (member_id NUMBER PRIMARY KEY, birth_ymd VARCHAR2(8));
INSERT INTO members VALUES (1, '19900512');
INSERT INTO members VALUES (2, '19850300');
COMMIT;Table created.
1 row created.
1 row created.
Commit complete.SELECT member_id, TO_CHAR(TO_DATE(birth_ymd, 'YYYYMMDD'), 'YYYY-MM-DD') AS birth_date FROM members ORDER BY member_id;SELECT member_id, TO_CHAR(TO_DATE(birth_ymd, 'YYYYMMDD'), 'YYYY-MM-DD') AS birth_date FROM members ORDER BY member_id
*
ERROR at line 1:
ORA-01847: day of month must be between 1 and last day of month
Help: https://docs.oracle.com/error-help/db/ora-01847/SELECT member_id, birth_ymd FROM members WHERE SUBSTR(birth_ymd, 7, 2) = '00'; MEMBER_ID BIRTH_YM
---------- --------
2 19850300
1 row selected.Treating '00' as the 1st is a business decision. If the day must stay unknown, return NULL for those rows instead.
SELECT member_id, TO_CHAR(TO_DATE(CASE WHEN SUBSTR(birth_ymd, 7, 2) = '00' THEN SUBSTR(birth_ymd, 1, 6) || '01' ELSE birth_ymd END, 'YYYYMMDD'), 'YYYY-MM-DD') AS birth_date FROM members ORDER BY member_id; MEMBER_ID BIRTH_DATE
---------- ----------
1 1990-05-12
2 1985-03-01
2 rows selected.02Next day calculated by adding 1 to the day digits of a string
closings.close_ymd had '20261130' and '20261231'. Adding 1 to the last two digits built '20261131' and '20261232'. Converting '20261232' (close 2) failed with ORA-01847.
Converting first and adding 1 to the DATE, TO_DATE(close_ymd, 'YYYYMMDD') + 1, returned 2026-12-01 and 2027-01-01. The month and year roll over correctly.
Setup
CREATE TABLE closings (close_id NUMBER PRIMARY KEY, close_ymd VARCHAR2(8));
INSERT INTO closings VALUES (1, '20261130');
INSERT INTO closings VALUES (2, '20261231');
COMMIT;Table created.
1 row created.
1 row created.
Commit complete.SELECT close_id, TO_DATE(SUBSTR(close_ymd, 1, 6) || LPAD(TO_NUMBER(SUBSTR(close_ymd, 7, 2)) + 1, 2, '0'), 'YYYYMMDD') AS next_day FROM closings WHERE close_id = 2;SELECT close_id, TO_DATE(SUBSTR(close_ymd, 1, 6) || LPAD(TO_NUMBER(SUBSTR(close_ymd, 7, 2)) + 1, 2, '0'), 'YYYYMMDD') AS next_day FROM closings WHERE close_id = 2
*
ERROR at line 1:
ORA-01847: day of month must be between 1 and last day of month
Help: https://docs.oracle.com/error-help/db/ora-01847/SELECT close_id, SUBSTR(close_ymd, 1, 6) || LPAD(TO_NUMBER(SUBSTR(close_ymd, 7, 2)) + 1, 2, '0') AS built_string FROM closings ORDER BY close_id; CLOSE_ID BUILT_STRING
---------- --------------------------------
1 20261131
2 20261232
2 rows selected.The string built for close 1, '20261131', isn't a real date either (November has 30 days). With DATE arithmetic both rows come out right.
SELECT close_id, TO_CHAR(TO_DATE(close_ymd, 'YYYYMMDD') + 1, 'YYYY-MM-DD') AS next_day FROM closings ORDER BY close_id; CLOSE_ID NEXT_DAY
---------- ----------
1 2026-12-01
2 2027-01-01
2 rows selected.03Loaded data contains a day like '2026-12-32'
stg_payments had '2026-12-01', '2026-12-32' and '2026-12-15'. INSERT INTO payments SELECT ..., TO_DATE(pay_dt, 'YYYY-MM-DD') failed with ORA-01847 and loaded nothing.
WHERE VALIDATE_CONVERSION(pay_dt AS DATE, 'YYYY-MM-DD') = 0 found payment 2. Loading only rows where it returns 1 inserted 2 rows.
Setup
CREATE TABLE stg_payments (pay_no NUMBER PRIMARY KEY, pay_dt VARCHAR2(10));
INSERT INTO stg_payments VALUES (1, '2026-12-01');
INSERT INTO stg_payments VALUES (2, '2026-12-32');
INSERT INTO stg_payments VALUES (3, '2026-12-15');
CREATE TABLE payments (pay_no NUMBER PRIMARY KEY, pay_date DATE);
COMMIT;Table created.
1 row created.
1 row created.
1 row created.
Table created.
Commit complete.INSERT INTO payments SELECT pay_no, TO_DATE(pay_dt, 'YYYY-MM-DD') FROM stg_payments;INSERT INTO payments SELECT pay_no, TO_DATE(pay_dt, 'YYYY-MM-DD') FROM stg_payments
*
ERROR at line 1:
ORA-01847: day of month must be between 1 and last day of month
Help: https://docs.oracle.com/error-help/db/ora-01847/SELECT pay_no, pay_dt FROM stg_payments WHERE VALIDATE_CONVERSION(pay_dt AS DATE, 'YYYY-MM-DD') = 0; PAY_NO PAY_DT
---------- ----------
2 2026-12-32
1 row selected.Rows that were skipped still need to be fixed at the source and loaded again.
INSERT INTO payments SELECT pay_no, TO_DATE(pay_dt, 'YYYY-MM-DD') FROM stg_payments WHERE VALIDATE_CONVERSION(pay_dt AS DATE, 'YYYY-MM-DD') = 1;
COMMIT;
SELECT COUNT(*) AS loaded FROM payments;2 rows created.
Commit complete.
LOADED
----------
2
1 row selected.FAQ
What does ORA-01847 day of month must be between 1 and last day of month mean?
The day part of the string is not a valid day number, such as 00 or 32.
What is the difference between ORA-01847 and ORA-01839?
ORA-01847 is for a day outside 1-31 ('20261232'). ORA-01839 is for a day inside 1-31 that the month doesn't have (April 31).
How do I add one day to a YYYYMMDD string in Oracle?
Convert first, then add: TO_DATE(col, 'YYYYMMDD') + 1. '20261231' became 2027-01-01.
How do I skip rows with invalid dates when loading?
Add WHERE VALIDATE_CONVERSION(col AS DATE, 'YYYY-MM-DD') = 1 to the INSERT ... SELECT, and check the rows where it is 0 separately.
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