Request a demo

BigQuery · Conversion function

BigQuery SAFE_CAST

SAFE_CAST(expression AS type) works like CAST but returns NULL instead of failing when a value can't be converted. Use it to clean messy strings without one bad row stopping the query, then count the NULLs it produced to see what failed.

GoogleSQL for BigQuery · Checked against the official reference in November 2026

SAFE_CAST takes the same arguments as CAST, including the FORMAT clause, and returns the same result whenever the conversion succeeds. When a value can't be converted ('n/a' to INT64, '2026-02-30' to DATE, an INT64 overflow, invalid UTF-8 bytes to STRING), it returns NULL for that row and the query keeps going. Snowflake and SQL Server call this TRY_CAST.

It only protects against failures at run time. A conversion that is never valid, such as DATE to INT64, still fails before the query runs with "Invalid cast from DATE to INT64". And it is a function of its own, not the SAFE. prefix: SAFE.CAST(...) is an error. The prefix is for other functions, such as SAFE.PARSE_DATE or SAFE.PARSE_JSON.

The catch is that failed conversions look exactly like real NULLs. Keep the raw column next to the converted one, and audit with WHERE raw IS NOT NULL AND SAFE_CAST(raw AS INT64) IS NULL to list the values that didn't convert.

Syntax

SAFE_CAST(expression AS type_name [FORMAT format_string])

Arguments and return value

ArgumentDescription
expressionThe value to convert.
type_nameThe target type, as for CAST.
format_stringOptional format model, as for CAST, for example 'DD/MM/YYYY'.

Returns: A value of type_name, or NULL if the conversion fails at run time.

Examples

Convert a messy text column

7.0 has a decimal point, which INT64 doesn't accept from a string.

WITH raw_orders AS (
  SELECT 1 AS row_id, '42' AS qty UNION ALL
  SELECT 2, '7.0' UNION ALL
  SELECT 3, 'n/a' UNION ALL
  SELECT 4, NULL
)
SELECT row_id, qty AS raw_qty, SAFE_CAST(qty AS INT64) AS qty
FROM raw_orders
ORDER BY row_id;
Result
row_idraw_qtyqty
14242
27.0NULL
3n/aNULL
4NULLNULL

List the values that failed

WITH raw_orders AS (
  SELECT '42' AS qty UNION ALL
  SELECT '7.0' UNION ALL
  SELECT 'n/a' UNION ALL
  SELECT 'n/a' UNION ALL
  SELECT NULL
)
SELECT qty AS bad_value, COUNT(*) AS rows_affected
FROM raw_orders
WHERE qty IS NOT NULL
  AND SAFE_CAST(qty AS INT64) IS NULL
GROUP BY qty
ORDER BY rows_affected DESC, bad_value;
Result
bad_valuerows_affected
n/a2
7.01

Impossible dates and other layouts

February 30 doesn't exist. A European date needs the FORMAT clause; without it, SAFE_CAST returns NULL.

SELECT
  SAFE_CAST('2026-02-30' AS DATE) AS impossible_date,
  SAFE_CAST('30/11/2026' AS DATE FORMAT 'DD/MM/YYYY') AS with_format,
  SAFE_CAST('30/11/2026' AS DATE) AS without_format;
Result
impossible_datewith_formatwithout_format
NULL2026-11-30NULL

Overflow and invalid bytes

An INT64 overflow returns NULL, but a FLOAT64 string beyond the type's range becomes Infinity rather than NULL.

SELECT
  SAFE_CAST('9223372036854775808' AS INT64) AS int64_overflow,
  SAFE_CAST('1e400' AS FLOAT64) AS float_overflow,
  SAFE_CAST(b'\xff' AS STRING) AS invalid_utf8;
Result
int64_overflowfloat_overflowinvalid_utf8
NULLInfinityNULL

Common mistakes

  • Treating every NULL as missing data. SAFE_CAST hides conversion failures; audit the raw values that turned into NULL.
  • Writing SAFE.CAST(x AS INT64). It fails with "Function not found"; SAFE_CAST is the function.
  • Expecting SAFE_CAST to understand other layouts. SAFE_CAST('30/11/2026' AS DATE) returns NULL, not a date; add FORMAT 'DD/MM/YYYY'.
  • Using it to turn decimal strings into integers. SAFE_CAST('7.0' AS INT64) returns NULL; convert through NUMERIC: SAFE_CAST(SAFE_CAST(x AS NUMERIC) AS INT64).
  • Expecting it to catch impossible type pairs. SAFE_CAST(DATE '2026-11-23' AS INT64) still fails before the query runs.

FAQ

What is the difference between CAST and SAFE_CAST in BigQuery?

CAST fails the query on the first value it can't convert. SAFE_CAST returns NULL for that value and continues. Both reject conversions that are never valid, like DATE to INT64.

What is the BigQuery equivalent of TRY_CAST?

SAFE_CAST. SAFE_CAST(x AS INT64) behaves like TRY_CAST(x AS INT) in Snowflake or SQL Server.

How do I find values that failed to convert?

Filter rows where the input is present but the result is NULL: WHERE raw IS NOT NULL AND SAFE_CAST(raw AS INT64) IS NULL.

Does SAFE_CAST support FORMAT?

Yes. SAFE_CAST('30/11/2026' AS DATE FORMAT 'DD/MM/YYYY') returns 2026-11-30, and NULL for strings that don't match the format.

Errors you may hit

All BigQuery conversion functions · All functions · BigQuery error messages

Observability for every BigQuery table, in about 15 minutes

Masthead detects freshness, volume and schema anomalies, monitors pipeline errors and maps column-level lineage across all your projects, from logs and metadata only.