Oracle 23.26Data typesLab-verified · 2026-11-25

ORA-01840: input value not long enough for date format

The string being converted to a date is shorter than the date format expects.

Oracle AI Database 26ai Free 23.26.3.0.0
ORA-01840: input value not long enough for date format

Jump to a cause

  1. 01A string date column has values of different lengths
  2. 02A DATE column compared to a partial date string like '2024-03'
  3. 03A year-month parameter '202403' converted with 'YYYYMMDD'

Lab-verifiedReproduced and fixed on Oracle AI Database 26ai Free 23.26.3.0.0 · 2026-11-25 · container image gvenzl/oracle-free:23-slim

TO_DATE (and Oracle's implicit string-to-date conversion) reads the input from left to right according to the format. If the string runs out before the format does, for example '202405' against 'YYYYMMDD', you get ORA-01840.

It shows up when old string date columns hold values of different lengths, when a DATE column is compared to a partial date like '2024-03', and when a year-month parameter is converted with a full-date format. All three were reproduced and fixed below.

01A string date column has values of different lengths

contracts.start_ymd VARCHAR2(8) held '20240301', '202405' and '20240615'. TO_DATE(start_ymd, 'YYYYMMDD') stopped the whole query with ORA-01840.

WHERE LENGTH(start_ymd) <> 8 found the culprit: contract 2, '202405', length 6. Choosing the format by length (CASE LENGTH(start_ymd) WHEN 8 THEN ... 'YYYYMMDD' WHEN 6 THEN ... 'YYYYMM') returned all three rows; the 6-digit value became 2024-05-01.

Setup
Setuprun as lab_a
CREATE TABLE contracts (contract_id NUMBER, start_ymd VARCHAR2(8));
INSERT INTO contracts VALUES (1, '20240301');
INSERT INTO contracts VALUES (2, '202405');
INSERT INTO contracts VALUES (3, '20240615');
COMMIT;
OUTPUT
Table created.


1 row created.


1 row created.


1 row created.


Commit complete.
Reproducerun as lab_a
SELECT contract_id, TO_DATE(start_ymd, 'YYYYMMDD') AS start_date FROM contracts ORDER BY contract_id;
✕ ERROR
SELECT contract_id, TO_DATE(start_ymd, 'YYYYMMDD') AS start_date FROM contracts ORDER BY contract_id
                            *
ERROR at line 1:
ORA-01840: input value not long enough for date format
Help: https://docs.oracle.com/error-help/db/ora-01840/
Checkrun as lab_a
COLUMN start_ymd FORMAT A9
SELECT contract_id, start_ymd, LENGTH(start_ymd) AS len FROM contracts WHERE LENGTH(start_ymd) <> 8;
OUTPUT
CONTRACT_ID START_YMD        LEN
----------- --------- ----------
          2 202405             6

1 row selected.

Run the LENGTH check first to see which lengths actually exist. 'YYYYMM' gives the first day of the month.

After the fixrun as lab_a
SELECT contract_id,
       TO_CHAR(CASE LENGTH(start_ymd)
                 WHEN 8 THEN TO_DATE(start_ymd, 'YYYYMMDD')
                 WHEN 6 THEN TO_DATE(start_ymd, 'YYYYMM')
               END, 'YYYY-MM-DD') AS start_date
FROM contracts ORDER BY contract_id;
✓ SUCCESS
CONTRACT_ID START_DATE
----------- ----------
          1 2024-03-01
          2 2024-05-01
          3 2024-06-15

3 rows selected.

02A DATE column compared to a partial date string like '2024-03'

With NLS_DATE_FORMAT = 'YYYY-MM-DD', WHERE sale_date >= '2024-03' failed with ORA-01840. Oracle converts '2024-03' to a date using the session format, and the day part is missing.

Using date literals for a month range, sale_date >= DATE '2024-03-01' AND sale_date < DATE '2024-04-01', returned 500 (the 03-05 and 03-31 rows), leaving out 02-28 and 04-01.

Setup
Setuprun as lab_a
CREATE TABLE sales (sale_id NUMBER, sale_date DATE, amount NUMBER);
INSERT INTO sales VALUES (1, DATE '2024-02-28', 100);
INSERT INTO sales VALUES (2, DATE '2024-03-05', 200);
INSERT INTO sales VALUES (3, DATE '2024-03-31', 300);
INSERT INTO sales VALUES (4, DATE '2024-04-01', 400);
COMMIT;
OUTPUT
Table created.


1 row created.


1 row created.


1 row created.


1 row created.


Commit complete.
Reproducerun as lab_a
ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD';
SELECT SUM(amount) FROM sales WHERE sale_date >= '2024-03';
✕ ERROR
Session altered.

SELECT SUM(amount) FROM sales WHERE sale_date >= '2024-03'
                                                 *
ERROR at line 1:
ORA-01840: input value not long enough for date format
Help: https://docs.oracle.com/error-help/db/ora-01840/
Checkrun as lab_a
SELECT TO_CHAR(DATE '2024-03-01', 'YYYY-MM-DD') AS month_start,
       TO_CHAR(ADD_MONTHS(DATE '2024-03-01', 1), 'YYYY-MM-DD') AS next_month_start
FROM dual;
OUTPUT
MONTH_STAR NEXT_MONTH
---------- ----------
2024-03-01 2024-04-01

1 row selected.

DATE literals don't depend on the session's NLS_DATE_FORMAT, so the query works the same in any session.

After the fixrun as lab_a
SELECT SUM(amount) AS march_total FROM sales
WHERE sale_date >= DATE '2024-03-01' AND sale_date < DATE '2024-04-01';
✓ SUCCESS
MARCH_TOTAL
-----------
        500

1 row selected.

03A year-month parameter '202403' converted with 'YYYYMMDD'

A monthly report took :p_ym = '202403' and used TO_DATE(:p_ym, 'YYYYMMDD'). The query failed with ORA-01840 because the value has no day.

TO_DATE('202403', 'YYYYMM') returns 2024-03-01, so switching the format to 'YYYYMM' (with ADD_MONTHS(..., 1) as the upper bound) returned 3000 for March (03-01 and 03-31, not 04-02).

Setup
Setuprun as lab_a
CREATE TABLE journal (je_id NUMBER, je_date DATE, amount NUMBER);
INSERT INTO journal VALUES (1, DATE '2024-03-01', 1000);
INSERT INTO journal VALUES (2, DATE '2024-03-31', 2000);
INSERT INTO journal VALUES (3, DATE '2024-04-02', 5000);
COMMIT;
OUTPUT
Table created.


1 row created.


1 row created.


1 row created.


Commit complete.
Reproducerun as lab_a
VARIABLE p_ym VARCHAR2(6)
EXEC :p_ym := '202403'
SELECT SUM(amount) FROM journal
WHERE je_date >= TO_DATE(:p_ym, 'YYYYMMDD')
  AND je_date < ADD_MONTHS(TO_DATE(:p_ym, 'YYYYMMDD'), 1);
✕ ERROR
PL/SQL procedure successfully completed.

  AND je_date < ADD_MONTHS(TO_DATE(:p_ym, 'YYYYMMDD'), 1)
                                    *
ERROR at line 3:
ORA-01840: input value not long enough for date format
Help: https://docs.oracle.com/error-help/db/ora-01840/
Checkrun as lab_a
SELECT TO_CHAR(TO_DATE('202403', 'YYYYMM'), 'YYYY-MM-DD') AS first_day FROM dual;
OUTPUT
FIRST_DAY
----------
2024-03-01

1 row selected.
After the fixrun as lab_a
VARIABLE p_ym VARCHAR2(6)
EXEC :p_ym := '202403'
SELECT SUM(amount) AS month_total FROM journal
WHERE je_date >= TO_DATE(:p_ym, 'YYYYMM')
  AND je_date < ADD_MONTHS(TO_DATE(:p_ym, 'YYYYMM'), 1);
✓ SUCCESS
PL/SQL procedure successfully completed.


MONTH_TOTAL
-----------
       3000

1 row selected.

FAQ

What does ORA-01840 input value not long enough for date format mean?

The string passed to TO_DATE (or implicitly converted) ends before the format does, for example '202405' with 'YYYYMMDD'.

How do I find the rows that cause ORA-01840?

Check the lengths: SELECT ... WHERE LENGTH(col) <> 8 (use the length your format expects). The rows it returns are the ones that don't fit.

Why does WHERE date_col >= '2024-03' give ORA-01840?

Oracle converts '2024-03' to a DATE with the session's NLS_DATE_FORMAT, and the day is missing. Use DATE '2024-03-01' and < DATE '2024-04-01' instead.

How do I convert '202403' (year and month) to a date?

Use TO_DATE('202403', 'YYYYMM'). It returns the first day of that month, 2024-03-01.

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