// DuckDB error reference

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
Categoría
Conversion and range errors
Mensaje de error
Conversion Error: invalid date field format: "not a date", expected format is (YYYY-MM-DD)

Qué significa

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.

Causas comunes

  • 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');

Cómo solucionarlo

  1. 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;
  2. Parse a known nonstandard layout with strptime:

    SELECT strptime(raw_date, '%d/%m/%Y')::DATE FROM stage;
  3. 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;
  4. 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.

Reprodúcelo de forma segura

SELECT 'not a date'::DATE;   -- Conversion Error: invalid date field format: "not a date", expected format is (YYYY-MM-DD)

Preguntas

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.

Errores relacionados

El código y el texto del mensaje son de DuckDB documentation. Explora la lista completa de DuckDB error list o buscar cada error de base de datos.

// menos errores, correcciones más rápidas

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.

Texto y datos de esta página: CC BY 4.0, con atribución a SQLly (sqlly.app). Código y fragmentos SQL: MIT. Detalles de la licencia El texto citado de los mensajes de error pertenece a su proveedor de base de datos.