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
它的意義
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 (或者 timestamp field value out of range). Arithmetic overflow gives Out of Range Error 。
常見原因
- 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');
如何修正
-
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.
安全地重現問題
SELECT 'abc'::INTEGER; -- Conversion Error: Could not convert string 'abc' to INT32
問題
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.
相關錯誤
Conversion ErrorThe value is out of range for INT32Binder ErrorDuckDB could not plan the query22P02PostgreSQL:類型的輸入語法無效22003PostgreSQL:數值超出範圍SQLITE_ERRORSQLite: SQL logic error
More conversion and range errors errors
程式碼和訊息文字來自 DuckDB documentation。瀏覽完整的 DuckDB error list 或 搜尋每個資料庫錯誤.
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.
本頁文字與資料: CC BY 4.0,請註明出處 SQLly (sqlly.app)。 程式碼與 SQL 片段: MIT. 授權詳情 引用的錯誤訊息文字歸其資料庫廠商所有。