// 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
カテゴリ
Conversion and range errors
エラーメッセージ
Conversion Error: Could not convert string 'abc' to INT32

意味

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

修正方法

  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.

安全に再現する

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.

関連エラー

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. ライセンスの詳細 引用したエラーメッセージの文言は、各データベースベンダーに帰属します。