BigQuery error · Data and type conversion
Bad int64 value
CAST(... AS INT64) received a string that isn't a whole number, such as "3.7", "1,200", "N/A" or an empty string, and the whole query failed. Use SAFE_CAST to get NULL for bad values, clean the string first, or cast through NUMERIC when decimals are expected.
The error message
Bad int64 value: <value>
Causes
Strings that aren't integers
Decimals (3.7), thousands separators (1,200), currency symbols, placeholders like N/A or -, and empty strings all fail. The empty string gives Bad int64 value: with nothing after the colon.
Numbers outside the INT64 range
A string such as 9223372036854775808 (one above the INT64 maximum) also reports Bad int64 value, which often points to IDs that should stay STRING.
Related messages for other types
The same failure for other targets reads Bad double value: abc (FLOAT64), Invalid NUMERIC value: abc, Invalid BIGNUMERIC value: abc and Bad bool value: yes (BOOL).
How to fix it
1. Use SAFE_CAST and look at what fails
SAFE_CAST returns NULL instead of failing. Pair it with a check so bad values stay visible.
WITH raw_orders AS (
SELECT '42' AS quantity UNION ALL
SELECT ' 7 ' UNION ALL
SELECT '3.7' UNION ALL
SELECT ''
)
SELECT
quantity,
SAFE_CAST(quantity AS INT64) AS quantity_int
FROM raw_orders;2. Clean the string before casting
Remove separators and symbols with REPLACE or REGEXP_REPLACE, and turn placeholders into NULL.
SELECT
SAFE_CAST(REGEXP_REPLACE(NULLIF(TRIM('$1,200'), 'N/A'), r'[^0-9-]', '') AS INT64) AS amount;3. Cast decimals through NUMERIC or FLOAT64
If values like 3.7 are legitimate, cast to NUMERIC (or FLOAT64) first; casting that to INT64 rounds half away from zero.
SELECT CAST(CAST('3.7' AS NUMERIC) AS INT64) AS rounded_quantity;How to prevent it
- Load raw files into STRING columns and convert with
SAFE_CASTin a modeling step that counts rejected values. - Keep identifiers (order numbers, phone numbers, ZIP codes) as STRING: they aren't quantities and can exceed or lose leading zeros in INT64.
- A runtime cast error can come after a long scan. On-demand the failed query isn't billed, but under capacity pricing the slots it used count, and scheduled retries fail the same way.
FAQ
Does CAST(' 42 ' AS INT64) work?
Yes, it returns 42: surrounding spaces are accepted. Hexadecimal strings work too, so CAST('0x1A' AS INT64) returns 26.
What is the PostgreSQL or Snowflake equivalent of SAFE_CAST?
Snowflake has TRY_CAST and TRY_TO_NUMBER. PostgreSQL has no built-in safe cast; you validate with a regex first. In BigQuery, SAFE_CAST or the SAFE. prefix on functions plays that role.
Why does WHERE is_numeric AND CAST(x AS INT64) > 0 still fail?
BigQuery doesn't guarantee the order in which it evaluates the operands of AND and OR, so the cast can run on rows the other condition was meant to exclude. Use SAFE_CAST, or put the cast inside IF or CASE, where errors in branches that aren't chosen are ignored.
Related
SQL reference
More data and type conversion
All BigQuery error messages · What 50M daily events say about BigQuery failures
