SQL error · The query will not run

Cannot convert text to date

Problem: The text holds an impossible date, an ambiguous format or a value that is not a date at all.

Solution: Validate the calendar and the format in a staging area before converting and loading the data.

Why it happens: cannot convert the text to a date

This usually comes up when importing a CSV, cleaning legacy data or converting a text column before comparing dates. This expression reproduces the problem on engines with a strict date type:

Invalid date value
CAST('2026-13-01' AS DATE)

The external format looks like YYYY-MM-DD, but the month 13 does not exist in the calendar. PostgreSQL and SQL Server reject values they cannot interpret as valid dates. In MySQL the outcome may depend on the SQL mode. SQLite is not directly comparable: it normally stores dates as text, a Julian day number or a Unix timestamp, and its date functions interpret those values.

“Cannot convert the text to a date” error messages by engine

The exact wording depends on the database. Compare these common messages with the one in your console.

EngineTypical message
PostgreSQLERROR: date/time field value out of range: "2026-13-01"
MySQLERROR 1292 (22007): Incorrect date value: '2026-13-01' for column 'date' at row 1
SQL ServerMsg 241, Level 16: Conversion failed when converting date and/or time from character string.
OracleORA-01843: not a valid month (también ORA-01861: literal does not match format string)

SQLite does not validate dates when storing them: date('2026-13-01') returns NULL without an error, and date('2025-02-30') returns 2025-03-02, a different date.

How to fix it in SQL

SQLite has no separate DATE type. For the practice on this site we can use date() to check for values the function does not recognise. With an impossible month, it returns NULL:

Finding text that cannot be interpreted
WITH imported(id, date_text) AS (
  VALUES
    (1, '2026-09-15'),
    (2, '2026-13-01'),
    (3, '2026-10-03')
)
SELECT id, date_text
FROM imported
WHERE date(date_text) IS NULL;
Result verified locally
iddate_text
22026-13-01

This check helps detect entries that SQLite clearly cannot interpret, but it should not become a promise of universal validation. Watch out: not every impossible date returns NULL. date('2025-02-30') returns 2025-03-02 because SQLite rolls 30 February over into the next month. To catch those cases too, check that date(date_text) = date_text, and also validate the business rules and the agreed format.

How to check it

Try a valid date, an impossible month and an ambiguous format. Keep the original text of the rejected rows.

How to avoid it next time

YYYY-MM-DD avoids ambiguity in the format, but it does not guarantee a valid date.

Related cases worth checking

Three separate problems that are best diagnosed one by one

ProblemExampleWhat to check
Impossible date2026-13-01Range of month, day and year
Ambiguous format03/04/2026Day/month order and regional settings
Text that is not a datependingSource, cleaning and column mapping

Do not fix them all the same way. Changing the separators does not fix a 13th month, and accepting an ambiguous string without knowing its format can produce a date that is valid but wrong.

Mental model: first decide which format the source promises; then check that the parts make up a real date; finally, convert to the engine's date type or representation.

First line of defence: standardise the input contract

For exchanging dates without a time, YYYY-MM-DD is a clear representation and avoids most of the day/month ambiguity. Even so, the visual pattern is not enough: 2026-13-01 is still invalid.

If you control the import, avoid mixing formats such as 31/12/2026, 12-31-2026 and 2026-12-31 in the same column. Keep the original text in a staging area when you need to audit which rows could not be converted.

How conversion differs between engines

PostgreSQL

Date/time types interpret the string according to their input rules and the setting DateStyle when the order is ambiguous. If a month or day field is out of range, the parser rejects the value. For data you exchange, use an unambiguous form and avoid depending on session settings.

MySQL 8.4

How invalid dates behave depends on the SQL mode. In strict mode, invalid date values can raise an error; with more permissive settings they can be converted to zero values or produce warnings. That is why a load that “works” on one installation does not guarantee the same result on another.

SQL Server

Conversions to date reject values they do not recognise as valid dates. If you are cleaning a text column and want to find failures without aborting the whole query, TRY_CONVERT returns NULL when an allowed conversion does not succeed:

SQL Server · tolerant conversion
SELECT TRY_CONVERT(date, date_text, 23) AS date
FROM imported;

The style 23 corresponds to the form yyyy-mm-dd. You can then review the rows whose result is NULL before inserting them into the final table.

SQLite

SQLite has no dedicated date/time type. Its functions accept specific date representations and return text or numbers depending on the function. Do not use CAST(... AS DATE) as though it were equivalent to PostgreSQL or SQL Server: for this purpose, work with date(), datetime() or a consistent ISO-8601 representation.