BigQuery Convert String to Number: CAST & SAFE_CAST

How-to basics · Updated November 23, 2026

To convert a string to a number in BigQuery, use the CAST function, for example CAST('123' AS INT64) or CAST('12.5' AS FLOAT64). For safer conversion that returns NULL instead of an error when the string isn’t numeric, use SAFE_CAST.

Why String-to-Number Conversion Matters

BigQuery stores data in strongly typed columns, but raw data ingestion often brings numbers as strings—from CSV files, JSON payloads, or user input. If you try to run arithmetic or aggregation on a string column, you’ll get an error like “No matching signature for operator +” or a query that silently treats values as text. Converting strings to numeric types is essential for calculations, comparisons, and efficient storage.

Core Functions: CAST and SAFE_CAST

The standard SQL CAST function is the primary tool. Its syntax is CAST(expression AS target_type). BigQuery supports these numeric target types:

  • INT64 – for whole numbers (e.g., 42, -7)
  • FLOAT64 – for floating-point numbers (e.g., 3.14, -0.001)
  • NUMERIC – for exact decimal with 38 digits of precision
  • BIGNUMERIC – for larger exact decimals

Example: SELECT CAST('123' AS INT64) AS int_value; returns 123 as an integer. For decimals: SELECT CAST('12.5' AS FLOAT64) AS float_value; returns 12.5.

SAFE_CAST works exactly like CAST but returns NULL instead of throwing an error if the conversion fails. For instance, SAFE_CAST('abc' AS INT64) returns NULL, while CAST('abc' AS INT64) would fail the query. Use SAFE_CAST when you expect dirty data and want to filter or handle invalid rows gracefully.

Handling Common String Formats

Strings often include extra characters like commas, currency symbols, or whitespace. CAST only works if the string is a clean numeric literal. For messy strings, you need to clean them first using string functions:

  • TRIM(value) – removes leading/trailing spaces.
  • REPLACE(value, ',', '') – removes commas (e.g., “1,234” becomes “1234”).
  • REPLACE(value, '$', '') – removes dollar signs.

Example: SELECT CAST(REPLACE(TRIM(' $1,234.50 '), ',', '') AS FLOAT64); returns 1234.5.

For scientific notation like “1.5E3”, CAST handles it directly: CAST('1.5E3' AS FLOAT64) returns 1500.0.

Choosing the Right Numeric Type

Selecting between INT64, FLOAT64, NUMERIC, and BIGNUMERIC depends on your data:

  • Use INT64 for whole numbers that fit within ±9.2 quintillion (64-bit range).
  • Use FLOAT64 for approximate decimals, especially when you need scientific notation or very large/small magnitudes.
  • Use NUMERIC for exact decimal arithmetic, such as financial amounts, where rounding errors are unacceptable. It supports up to 38 digits of precision.
  • Use BIGNUMERIC for even larger exact numbers beyond NUMERIC‘s range.

If you’re unsure, FLOAT64 is the most flexible for general use, but for money, always use NUMERIC to avoid floating-point inaccuracies.

Common Errors and How to Avoid Them

Even with the right function, you can hit pitfalls:

  • Leading/trailing spaces: CAST(' 123' AS INT64) fails. Always TRIM first.
  • Commas or currency symbols: Remove them with REPLACE before casting.
  • Empty strings: CAST('' AS INT64) errors. Use NULLIF(value, '') to convert empty strings to NULL first.
  • Locale-specific formats: BigQuery expects a dot as decimal separator. If your data uses commas for decimals (e.g., “1,5”), replace the comma with a dot: REPLACE(value, ',', '.').
  • Non-numeric text: Use SAFE_CAST to avoid query failure, then filter out NULLs.

Example of a robust conversion: SELECT SAFE_CAST(NULLIF(TRIM(REPLACE(value, ',', '')), '') AS FLOAT64) FROM dataset.table;

Performance and Efficiency Tips

Converting strings to numbers in a WHERE clause can prevent partition pruning and slow down queries. If you frequently filter on a numeric value stored as a string, consider storing the data as the correct type in a new column or using a view that casts once. Also, avoid casting inside functions like SUM(CAST(column AS INT64)) if you can cast once in a subquery. BigQuery charges per byte processed, so converting large string columns repeatedly can increase cost.

Alternatives to CAST

While CAST is the standard, there are other ways:

  • PARSE_NUMERIC – converts a string to NUMERIC using a format string, e.g., PARSE_NUMERIC('$1,234.50', '\$9,999.99'). Useful for complex formats.
  • PARSE_BIGNUMERIC – similar for BIGNUMERIC.
  • FLOAT64() – a shorthand for CAST(x AS FLOAT64) in some contexts, but CAST is clearer.

For Google Sheets users, the equivalent is VALUE() or NUMBERVALUE(), but in BigQuery, CAST is the go-to.

Frequently Asked Questions

Can I convert a string with a comma as decimal separator?

Yes, but you must first replace the comma with a dot: CAST(REPLACE('1,5', ',', '.') AS FLOAT64) returns 1.5. BigQuery does not automatically handle locale-specific separators.

What happens if I cast a non-numeric string?

CAST will throw an error and fail the query. SAFE_CAST returns NULL instead, allowing you to continue processing other rows.

Is there a way to convert to an integer and round decimals?

Use CAST(CAST('12.7' AS FLOAT64) AS INT64) which truncates toward zero (gives 12). For rounding, use ROUND(CAST('12.7' AS FLOAT64)) which returns a FLOAT64 rounded to nearest integer.

Does CAST work on arrays or structs?

Yes, you can cast arrays element-wise using ARRAY(SELECT CAST(x AS INT64) FROM UNNEST(array_column) AS x). For structs, cast each field individually.

Why does my CAST return NULL even with SAFE_CAST?

If the string is empty or contains only spaces, SAFE_CAST returns NULL. Use NULLIF(TRIM(value), '') to handle empty strings explicitly, but note SAFE_CAST already returns NULL for those cases.

Can I convert a string to a number in a view?

Yes, you can create a view that casts the column, but note that the view will be re-evaluated each time you query it. For performance, consider a materialized table if the data changes infrequently.

Related

Try it: the Date Difference Calculator: days, months, years and weekdays between two dates, with the DATEDIF formulas.

More in How-to basics

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.