Conversion Error: The value is out of range for INT32
A cast met a number the destination type cannot hold, for example 99999999999 cast to INTEGER.
- Exception
Conversion Error
意味
A cast reached a value that fits the source type but not the destination: 99999999999 is a valid BIGINT, and casting it to INTEGER fails because INTEGER stops at 2147483647. The message names the physical types: INT64 is BIGINT and INT32 is INTEGER, so it tells you both the value and the type that refused it.
Keep this class apart from its neighbors. Could not convert string 'abc' to INT32 means the value is the wrong kind, and Out of Range Error: Overflow in multiplication of INT8 means arithmetic overflowed after the fact. Here the value exists, the cast is the right idea, and the destination type is too small.
よくある原因
- A literal or computed value wider than the column type, for example loading BIGINT counts into an INTEGER column.
- A CSV or Parquet import where the column was declared or inferred narrower than the data it holds.
- A DECIMAL(4,2) column receiving 100.00, which needs three digits before the decimal point.
Find the cause
-- The declared type of the target column: DESCRIBE my_table; -- How far out of range the source values are, and how many: SELECT min(amount), max(amount), count(*) FROM stage WHERE TRY_CAST(amount AS INTEGER) IS NULL AND amount IS NOT NULL;
修正方法
-
Count and inspect the values that will not fit before changing anything:
SELECT count(*) FROM stage WHERE amount IS NOT NULL AND TRY_CAST(amount AS INTEGER) IS NULL;
-
Widen the column to the smallest type that holds the real range:
ALTER TABLE measurements ALTER COLUMN amount TYPE BIGINT;
-
For imports, declare the type up front so the scan does not decide for you:
SELECT * FROM read_csv('input.csv', types = {'amount': 'BIGINT'}); -
When out-of-range rows are expected, sort them out with TRY_CAST instead of failing the load:
SELECT TRY_CAST(amount AS INTEGER) FROM stage;
Other ways to handle it
Widening the column is usually right because the data is what it is. Clamping with least and greatest, or dropping rows via TRY_CAST, silently changes data, so only do it with a rule the owner of the numbers would accept.
安全に再現する
SELECT 99999999999::INTEGER; -- Conversion Error: Type INT64 with value 99999999999 can't be cast because the value is out of range for the destination type INT32
質問
Why does the message say INT32 when my column is INTEGER?
DuckDB messages name physical types: INTEGER is INT32, BIGINT is INT64, TINYINT is INT8. Read INT32 as INTEGER.
Can I get NULL instead of an error for bad values?
Use TRY_CAST: it returns NULL when the value does not fit, so you can filter or count the out-of-range rows instead of failing the statement.
関連エラー
Conversion ErrorThe value cannot be cast to the target typeConversion ErrorThe date or timestamp string is malformed22003PostgreSQL: 数値が範囲外です
コードとメッセージのテキストは、 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. ライセンスの詳細 引用したエラーメッセージの文言は、各データベースベンダーに帰属します。