BigQuery · Conversion function
BigQuery CAST
CAST(expression AS type) converts a value to another data type, such as STRING to INT64 or TIMESTAMP to DATE, and fails the whole query if any value can't be converted. Add a FORMAT clause to parse or print dates, numbers and bytes in a specific layout.
CAST is BigQuery's explicit conversion: CAST('42' AS INT64), CAST(order_id AS STRING), CAST(ts AS DATE). Target types use BigQuery names (INT64, FLOAT64, NUMERIC, BIGNUMERIC, STRING, BYTES, BOOL, DATE, DATETIME, TIME, TIMESTAMP), and aliases such as INTEGER and DECIMAL also work. The PostgreSQL shorthand '42'::INT64 is rejected with "CAST operators are not supported", and so is a parameterized target such as NUMERIC(10, 2): "Parameterized types are not allowed in CAST expressions".
If a single row can't be converted, the whole query fails, with errors such as Bad int64 value or "Invalid date". When the input can be dirty, SAFE_CAST returns NULL for those rows instead. A conversion that is never valid, such as DATE to INT64, fails before the query runs.
A few conversion rules catch people out. FLOAT64 and NUMERIC to INT64 round half away from zero, so 2.5 becomes 3. STRING to INT64 accepts surrounding spaces, a sign and hex ('0x1F'), but not a decimal point ('12.0'). STRING to BOOL accepts only 'true' and 'false', in any letter case. STRING to DATE needs year-month-day order with dashes. TIMESTAMP to DATE or STRING uses UTC, and CAST(ts AS STRING) ends in +00.
The FORMAT clause handles layouts the default rules don't: CAST('23/11/2026' AS DATE FORMAT 'DD/MM/YYYY') parses a European date, CAST(d AS STRING FORMAT 'Mon DD, YYYY') prints one, and CAST(x AS STRING FORMAT 'FM9,999,999.99') adds thousands separators. When formatting a TIMESTAMP as STRING, AT TIME ZONE 'Asia/Tokyo' prints local time instead of UTC.
Syntax
CAST(expression AS type_name [FORMAT format_string])
CAST(expression AS { STRING | TIMESTAMP } FORMAT format_string AT TIME ZONE time_zone)Arguments and return value
| Argument | Description |
|---|---|
expression | The value to convert. |
type_name | The target type, for example INT64, NUMERIC, STRING, DATE or TIMESTAMP. Parameterized types such as NUMERIC(10, 2) are not allowed. |
format_string | Optional format model such as 'DD/MM/YYYY' or 'FM9,999.99'. Allowed when parsing a STRING into DATE, DATETIME, TIME, TIMESTAMP or BYTES, and when formatting numbers, BYTES, dates and times as STRING. |
time_zone | Optional, only with FORMAT: the time zone used to format a TIMESTAMP as STRING, or to read a STRING into a TIMESTAMP. Default UTC. |
Returns: A value of type_name.
Examples
Common conversions
FLOAT64 to INT64 rounds half away from zero in both directions.
SELECT
CAST('42' AS INT64) AS qty,
CAST('19.99' AS NUMERIC) AS price,
CAST('2026-11-23' AS DATE) AS order_date,
CAST(1001 AS STRING) AS order_ref,
CAST(2.5 AS INT64) AS half_up,
CAST(-2.5 AS INT64) AS half_down;| qty | price | order_date | order_ref | half_up | half_down |
|---|---|---|---|---|---|
| 42 | 19.99 | 2026-11-23 | 1001 | 3 | -3 |
Parse and print with FORMAT
Without the FM prefix, numeric formats reserve a leading space for the sign and pad to the format's width.
SELECT
CAST('23/11/2026' AS DATE FORMAT 'DD/MM/YYYY') AS parsed_date,
CAST(DATE '2026-11-23' AS STRING FORMAT 'Mon DD, YYYY') AS date_label,
CAST(1234567.891 AS STRING FORMAT 'FM9,999,999.99') AS amount_label;| parsed_date | date_label | amount_label |
|---|---|---|
| 2026-11-23 | Nov 23, 2026 | 1,234,567.89 |
Timestamps convert in UTC unless you say otherwise
23:30 UTC is already the next morning in Tokyo.
SELECT
CAST(TIMESTAMP '2026-11-23 23:30:00 UTC' AS DATE) AS utc_date,
DATE(TIMESTAMP '2026-11-23 23:30:00 UTC', 'Asia/Tokyo') AS tokyo_date,
CAST(TIMESTAMP '2026-11-23 23:30:00 UTC' AS STRING FORMAT 'YYYY-MM-DD HH24:MI TZH:TZM' AT TIME ZONE 'Asia/Tokyo') AS tokyo_time;| utc_date | tokyo_date | tokyo_time |
|---|---|---|
| 2026-11-23 | 2026-11-24 | 2026-11-24 08:30 +09:00 |
What CAST accepts from strings
CAST('12.0' AS INT64) fails with Bad int64 value: 12.0, but going through NUMERIC works. Spaces, hex and upper-case booleans are accepted.
SELECT
CAST(CAST('12.0' AS NUMERIC) AS INT64) AS qty_from_decimal_string,
CAST(' 42 ' AS INT64) AS padded,
CAST('0x1F' AS INT64) AS hex_value,
CAST('TRUE' AS BOOL) AS bool_value;| qty_from_decimal_string | padded | hex_value | bool_value |
|---|---|---|---|
| 12 | 42 | 31 | true |
Common mistakes
- Casting decimal strings straight to INT64.
CAST('12.0' AS INT64)fails; useCAST(CAST(x AS NUMERIC) AS INT64), or SAFE_CAST for dirty data. - Expecting truncation from FLOAT64 to INT64. CAST rounds half away from zero; apply TRUNC or FLOOR first if you need to cut off the fraction.
- Getting a UTC date by accident.
CAST(ts AS DATE)uses UTC; for a local calendar date useDATE(ts, 'America/New_York'). - Porting
::casts,CAST(x AS DECIMAL(10, 2)),TO_NUMBERorTO_CHAR. Use plain CAST with an unparameterized type, ROUND for the scale, and the FORMAT clause for layouts. - Comparing a DATE column with a malformed string literal. BigQuery converts the literal implicitly and fails with Could not cast literal before the query runs.
Cost and performance
- Keep partition and cluster columns uncast in filters.
WHERE CAST(order_date AS STRING) = '2026-11-23'can stop partition pruning and bill the whole table;WHERE order_date = '2026-11-23'compares against a literal that BigQuery converts to DATE, so only the matching partition is scanned.
FAQ
How do I convert a string to an integer in BigQuery?
CAST(x AS INT64). If some values aren't numbers, SAFE_CAST(x AS INT64) returns NULL for them, and for strings with a decimal point, cast to NUMERIC first.
How do I convert a timestamp to a date in BigQuery?
CAST(ts AS DATE) or DATE(ts), both in UTC. For the calendar date in a specific zone, use DATE(ts, 'Europe/Berlin'); see DATE.
How do I format a number with thousands separators?
FORMAT("%'.2f", amount) returns 1,234.50 for 1234.5. The FORMAT clause also works, CAST(amount AS STRING FORMAT '999,999.00'), but pads with leading spaces, and its FM prefix removes those spaces together with trailing zeros.
Does BigQuery support the :: cast operator?
No. '42'::INT64 fails with "CAST operators are not supported"; write CAST('42' AS INT64).
Related
Errors you may hit
All BigQuery conversion functions · All functions · BigQuery error messages
