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- Categoría
- Conversion and range errors
Qué significa
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 (o timestamp field value out of range). Arithmetic overflow gives Out of Range Error en su lugar.
Causas comunes
- 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');
Cómo solucionarlo
-
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]+$');
-
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;
-
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; -
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.
Reprodúcelo de forma segura
SELECT 'abc'::INTEGER; -- Conversion Error: Could not convert string 'abc' to INT32
Preguntas
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.
Errores relacionados
Conversion ErrorThe value is out of range for INT32Binder ErrorDuckDB could not plan the query22P02PostgreSQL: sintaxis de entrada no válida para el tipo22003PostgreSQL: valor numérico fuera de rangoSQLITE_ERRORSQLite: SQL logic error
More conversion and range errors errors
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.
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.