Conversion Error: The date or timestamp string is malformed
A string cast to DATE, TIME or TIMESTAMP did not match the format DuckDB expects.
- Exception
Conversion Error- Category
- Conversion and range errors
What it means
Casting text to a temporal type uses DuckDB's own expected layout: YYYY-MM-DD for dates and the ISO layout for timestamps. When a value does not fit, the error quotes the offending string and the expected format, and the whole statement stops on the first bad value.
A plain cast takes no format argument and is all-or-nothing. Text in another layout, such as 25/12/2024, is not read as a date at all: parse it with strptime, which takes an explicit format string, or with try_strptime, which returns NULL for values that do not match instead of failing.
Common causes
- A CSV column inferred as DATE or TIMESTAMP while some rows hold text in another layout.
- Day-first or month-first strings such as 25/12/2024, which no plain cast accepts.
- Locale month names such as Dec 25 2024, which no plain cast accepts.
- An empty string cast to DATE, which fails instead of becoming NULL.
Find the cause
-- Rows that refuse the plain cast, with the actual text:
SELECT raw_date FROM stage
WHERE raw_date IS NOT NULL AND TRY_CAST(raw_date AS DATE) IS NULL LIMIT 20;
-- What type a CSV scan would infer for the column:
DESCRIBE SELECT * FROM read_csv_auto('input.csv');
How to fix it
-
See how many values will not parse before changing the load:
SELECT count(*) FROM stage WHERE raw_date IS NOT NULL AND TRY_CAST(raw_date AS DATE) IS NULL;
-
Parse a known nonstandard layout with strptime:
SELECT strptime(raw_date, '%d/%m/%Y')::DATE FROM stage;
-
Read the file as text first and parse deliberately. This assumes every row uses the same layout. For a mixed column, use the try_strptime step below instead:
CREATE TABLE stage AS SELECT * FROM read_csv('input.csv', all_varchar = true); SELECT strptime(raw_date, '%d/%m/%Y')::DATE FROM stage; -
For values that may be dirty, use try_strptime and inspect what came back NULL:
SELECT raw_date FROM stage WHERE try_strptime(raw_date, '%d/%m/%Y') IS NULL AND raw_date IS NOT NULL;
Other ways to handle it
Fixing the format at the source beats fixing it in SQL: ask the producer for ISO dates if you can. When the file is the constraint, keep the column as VARCHAR in staging and parse once into a typed table, so the rule lives in one place.
Reproduce it safely
SELECT 'not a date'::DATE; -- Conversion Error: invalid date field format: "not a date", expected format is (YYYY-MM-DD)
Questions
What format does DuckDB expect for dates?
ISO: YYYY-MM-DD for DATE and YYYY-MM-DD HH:MM:SS for TIMESTAMP. Anything else needs strptime with an explicit format.
How do I handle a column with mixed date formats?
Normalize in staging: read as VARCHAR, try each format with coalesce(try_strptime(x, f1), try_strptime(x, f2)), and route what is left to a rejects list.
Related errors
Conversion ErrorThe value cannot be cast to the target typeConversion ErrorThe value is out of range for INT3222007PostgreSQL: invalid datetime format
The code and message text are from the DuckDB documentation. Browse the full DuckDB error list or search every database error.
Work on DuckDB in SQLly
A native SQL client for macOS, Windows and Linux with schema-aware IntelliSense, guardrails for risky statements and clear error messages, for DuckDB and the other databases you use.
Text and data on this page: CC BY 4.0, credit SQLly (sqlly.app). Code and SQL snippets: MIT. License details Quoted error message text belongs to its database vendor. View as Markdown