// DuckDB error reference

Conversion Error: The value cannot be cast to the target type

A cast or implicit conversion met a value it cannot represent in the destination type, such as 'abc' as an integer.

Exception
Conversion Error
Catégorie
Conversion and range errors
Message d'erreur
Conversion Error: Could not convert string 'abc' to INT32

Ce que cela signifie

DuckDB applies casts eagerly: a comparison between text and a number, a COPY into a typed table, or a CSV import all convert values, and the first bad value raises Conversion Error. The message shows the offending value and the destination type, for example Could not convert string 'abc' to INT32.

Conversion and parsing are related but distinct. Could not convert string covers plain casts; malformed dates give their own wording (invalid date field format: "31/12/2024", expected format is (YYYY-MM-DD)), malformed timestamps give invalid timestamp field format with their own expected format, and an impossible but well-formed value gives date field value out of range (ou timestamp field value out of range). Arithmetic overflow gives Out of Range Error à la place.

Causes courantes

  • A text value with letters, spaces or a wrong separator cast to a numeric type.
  • A CSV read with an explicit numeric column type, or a COPY into a typed table, where some rows hold text. Auto-detection does not fail on this: it infers VARCHAR for the column.
  • An implicit cast in a comparison where the two sides have different types, such as 'abc' = 5.
  • A malformed date, time or timestamp string that does not match the expected format.

Find the cause

-- Which rows hold values that will not convert? Count them before fixing:
SELECT count(*) FROM stage WHERE TRY_CAST(raw_value AS INTEGER) IS NULL AND raw_value IS NOT NULL;
-- A sample of the offending values:
SELECT raw_value FROM stage WHERE TRY_CAST(raw_value AS INTEGER) IS NULL AND raw_value IS NOT NULL LIMIT 20;
-- The inferred types a CSV read would use:
DESCRIBE SELECT * FROM read_csv_auto('input.csv');

Comment le corriger

  1. Find the bad values before casting, so the fix is data-driven:

    SELECT raw_value FROM stage
    WHERE raw_value IS NOT NULL AND NOT regexp_matches(raw_value, '^-?[0-9]+$');
  2. Cast with TRY_CAST, which returns NULL for values it cannot convert instead of failing the query:

    SELECT TRY_CAST(raw_value AS INTEGER) FROM stage;
  3. For CSV files, scan everything as VARCHAR first and convert explicitly with a rule:

    CREATE TABLE stage AS
    SELECT * FROM read_csv('input.csv', all_varchar = true);
    SELECT TRY_CAST(amount AS DECIMAL(12,2)) FROM stage;
  4. Parse nonstandard formats with a format hint rather than a plain cast:

    SELECT strptime(ts_text, '%d/%m/%Y %H:%M') FROM events;

Other ways to handle it

TRY_CAST is the quick fix, but it hides bad rows. For a pipeline, split the load: import as VARCHAR, log the rows that fail the cast, and insert only the clean ones into the typed table.

Reproduisez-le en toute sécurité

SELECT 'abc'::INTEGER;   -- Conversion Error: Could not convert string 'abc' to INT32

Questions

How do I import a CSV that has bad rows without failing the whole load?

Read with all_varchar = true into a staging table, then TRY_CAST into the typed table and review the rows that came out NULL.

Why does the message say INT32 when my column is INTEGER?

DuckDB stores INTEGER as a 32-bit integer, so messages name the physical type. INTEGER is INT32, BIGINT is INT64, TINYINT is INT8.

Erreurs liées

More conversion and range errors errors

Le code et le texte du message sont issus de DuckDB documentation. Parcourir la liste complète DuckDB error list ou Rechercher toutes les erreurs de base de données.

// moins d'erreurs, corrections plus rapides

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.

Texte et données de cette page : CC BY 4.0, en citant SQLly (sqlly.app). Code et extraits SQL : MIT. Détails de la licence Le texte des messages d’erreur cité appartient à l’éditeur de la base de données.