Lab-verifiedReproduced and fixed on Oracle AI Database 26ai Free 23.26.3.0.0 · 2026-11-27 · container image gvenzl/oracle-free:23-slim
ORA-01839 means the day is a normal number (1 to 31), but that month doesn't have it: April 31, February 30, or February 29 in a year that isn't a leap year. If the day itself is out of range (00 or 32), Oracle raises ORA-01847 instead.
It usually comes from building date strings by hand: gluing '31' onto a year-month for month-end, adding 1 to the year of a Feb 29 date, or loading source data that contains an impossible date. All three were reproduced and fixed below.
01Month-end built by appending '31' to the year-month
monthly_close.ym held '202603' and '202604'. TO_DATE(ym || '31', 'YYYYMMDD') failed with ORA-01839 because April has no 31st.
LAST_DAY(TO_DATE(ym, 'YYYYMM')) gives the real last day of each month: 31-MAR-26 and 30-APR-26.
Setup
CREATE TABLE monthly_close (ym VARCHAR2(6) PRIMARY KEY);
INSERT INTO monthly_close VALUES ('202603');
INSERT INTO monthly_close VALUES ('202604');
COMMIT;Table created.
1 row created.
1 row created.
Commit complete.SELECT ym, TO_DATE(ym || '31', 'YYYYMMDD') AS close_date FROM monthly_close ORDER BY ym;ERROR:
ORA-01839: date not valid for month specified
Help: https://docs.oracle.com/error-help/db/ora-01839/
no rows selectedCOLUMN last_dd FORMAT A7
SELECT ym, TO_CHAR(LAST_DAY(TO_DATE(ym, 'YYYYMM')), 'DD') AS last_dd FROM monthly_close ORDER BY ym;YM LAST_DD
------ -------
202603 31
202604 30
2 rows selected.LAST_DAY also handles February in leap years, so you never have to hard-code 28, 29, 30 or 31.
SELECT ym, LAST_DAY(TO_DATE(ym, 'YYYYMM')) AS close_date FROM monthly_close ORDER BY ym;YM CLOSE_DATE
------ ------------------
202603 31-MAR-26
202604 30-APR-26
2 rows selected.02Adding a year to February 29 by rebuilding the string
A contract started on 2024-02-29. The renewal date was built as (year + 1) || '-MM-DD', which made '2025-02-29', and TO_DATE failed with ORA-01839.
ADD_MONTHS(start_date, 12) returned 28-FEB-25. When the target month is shorter, ADD_MONTHS moves to its last day.
Setup
CREATE TABLE contracts (contract_id NUMBER PRIMARY KEY, start_date DATE);
INSERT INTO contracts VALUES (1, DATE '2024-02-29');
COMMIT;Table created.
1 row created.
Commit complete.SELECT contract_id, TO_DATE(TO_CHAR(EXTRACT(YEAR FROM start_date) + 1) || TO_CHAR(start_date, '-MM-DD'), 'YYYY-MM-DD') AS renew_date FROM contracts;SELECT contract_id, TO_DATE(TO_CHAR(EXTRACT(YEAR FROM start_date) + 1) || TO_CHAR(start_date, '-MM-DD'), 'YYYY-MM-DD') AS renew_date FROM contracts
*
ERROR at line 1:
ORA-01839: date not valid for month specified
Help: https://docs.oracle.com/error-help/db/ora-01839/SELECT contract_id, TO_CHAR(start_date, 'YYYY-MM-DD') AS start_date FROM contracts;CONTRACT_ID START_DATE
----------- ----------
1 2024-02-29
1 row selected.Do date arithmetic on DATE values (ADD_MONTHS, + n days), not on the string.
SELECT contract_id, ADD_MONTHS(start_date, 12) AS renew_date FROM contracts;CONTRACT_ID RENEW_DATE
----------- ------------------
1 28-FEB-25
1 row selected.03Loaded data contains an impossible date like '20260230'
stg_orders.order_dt VARCHAR2(8) had '20260115', '20260230' and '20260310'. TO_DATE(order_dt, 'YYYYMMDD') stopped the whole query with ORA-01839.
WHERE VALIDATE_CONVERSION(order_dt AS DATE, 'YYYYMMDD') = 0 found the bad row: order 2, '20260230'. TO_DATE(order_dt DEFAULT NULL ON CONVERSION ERROR, 'YYYYMMDD') returned all three rows, with NULL for order 2.
Setup
CREATE TABLE stg_orders (order_no NUMBER PRIMARY KEY, order_dt VARCHAR2(8));
INSERT INTO stg_orders VALUES (1, '20260115');
INSERT INTO stg_orders VALUES (2, '20260230');
INSERT INTO stg_orders VALUES (3, '20260310');
COMMIT;Table created.
1 row created.
1 row created.
1 row created.
Commit complete.SELECT order_no, TO_DATE(order_dt, 'YYYYMMDD') AS order_date FROM stg_orders ORDER BY order_no;SELECT order_no, TO_DATE(order_dt, 'YYYYMMDD') AS order_date FROM stg_orders ORDER BY order_no
*
ERROR at line 1:
ORA-01839: date not valid for month specified
Help: https://docs.oracle.com/error-help/db/ora-01839/SELECT order_no, order_dt FROM stg_orders WHERE VALIDATE_CONVERSION(order_dt AS DATE, 'YYYYMMDD') = 0; ORDER_NO ORDER_DT
---------- --------
2 20260230
1 row selected.Find and fix the source rows first. DEFAULT NULL ON CONVERSION ERROR hides bad values, so use it only when NULL is acceptable.
SELECT order_no, TO_DATE(order_dt DEFAULT NULL ON CONVERSION ERROR, 'YYYYMMDD') AS order_date FROM stg_orders ORDER BY order_no; ORDER_NO ORDER_DATE
---------- ------------------
1 15-JAN-26
2
3 10-MAR-26
3 rows selected.FAQ
What does ORA-01839 date not valid for month specified mean?
The day doesn't exist in that month, for example April 31 or February 29 in 2025. The format matched; the calendar didn't.
What is the difference between ORA-01839 and ORA-01847?
ORA-01847 is for a day outside 1-31 (like 00 or 32). ORA-01839 is for a day that is in 1-31 but not valid for that month (like 04-31 or 02-30).
How do I get the last day of the month in Oracle?
Use LAST_DAY(TO_DATE('202604', 'YYYYMM')). It returned 30-APR-26 for April and 31-MAR-26 for March.
How do I find rows with invalid dates in a string column?
Use WHERE VALIDATE_CONVERSION(col AS DATE, 'YYYYMMDD') = 0. It returns the rows that can't be converted instead of failing.
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