BigQuery · String function
BigQuery FORMAT
FORMAT(pattern, values...) builds a string with printf-style specifiers: FORMAT('%.2f', 3.14159) returns 3.14, FORMAT("%'d", 1234567) returns 1,234,567 and FORMAT('%s has %d orders', name, n) fills in text. Use %t to print any type.
FORMAT takes a format string and one argument per specifier. Each specifier starts with % and has the shape %[flags][width][.precision]specifier. The common ones: %d (or %i) for INT64, %s for STRING, %f, %e and %g for FLOAT64, NUMERIC and BIGNUMERIC, %x and %o for hex and octal, %t for a readable form of any type, %T for a SQL literal of any type, and %% for a literal percent sign.
Flags change the output: 0 pads with zeros (%06d), - left-aligns within the width (%-10s), + always prints the sign, and ' adds thousands separators (%'d, %'.2f). The width is a minimum, so long values are never cut; precision sets the digits after the decimal point for %f and the maximum length for %s and %t. A * takes the width or precision from an extra argument.
Types must match the specifier: %s rejects INT64 and %d rejects FLOAT64 and NUMERIC, both at compile time. A NULL argument makes the whole result NULL, except with %t and %T, which print NULL. FORMAT isn't locale-aware and doesn't format dates with patterns; for that, use FORMAT_DATE or FORMAT_TIMESTAMP and pass the result to %s.
Syntax
FORMAT(format_string_expression, data_type_expression[, ...])
%[flags][width][.precision]specifierArguments and return value
| Argument | Description |
|---|---|
format_string_expression | A STRING with zero or more format specifiers. A NULL format string returns NULL. |
data_type_expression | One value per specifier (two for specifiers with *), in order. The type must fit the specifier; %t and %T accept any type. |
Returns: STRING.
Examples
Numbers: decimals, separators, padding, percent
SELECT
FORMAT('%.2f', 3.14159) AS two_decimals,
FORMAT("%'d", 1234567) AS thousands,
FORMAT("%'.2f", 1234567.891) AS thousands_decimals,
FORMAT('%06d', 42) AS zero_padded,
FORMAT('%.1f%%', 12.345) AS percent;| two_decimals | thousands | thousands_decimals | zero_padded | percent |
|---|---|---|---|---|
| 3.14 | 1,234,567 | 1,234,567.89 | 000042 | 12.3% |
Text templates and alignment
SELECT
FORMAT('%s has %d open orders', 'Ana', 3) AS message,
FORMAT('[%-8s]', 'EU') AS left_aligned,
FORMAT('[%8s]', 'EU') AS right_aligned,
FORMAT('%t | %t', DATE '2026-11-23', [1, 2]) AS any_type,
FORMAT('%T', 'EU') AS sql_literal;| message | left_aligned | right_aligned | any_type | sql_literal |
|---|---|---|---|---|
| Ana has 3 open orders | [EU ] | [ EU] | 2026-11-23 | [1, 2] | "EU" |
NULL arguments
Only %t and %T keep the rest of the string when an argument is NULL.
SELECT
FORMAT('%d items', CAST(NULL AS INT64)) AS with_d,
FORMAT('%t items', CAST(NULL AS INT64)) AS with_t;| with_d | with_t |
|---|---|
| NULL | NULL items |
Readable cost report
The dollar figure uses the on-demand list price of $6.25 per TiB.
WITH jobs AS (
SELECT 'daily_orders' AS job_name, 1536 AS gib_billed UNION ALL
SELECT 'events_backfill', 20480
)
SELECT FORMAT("%-16s %'8d GiB $%.2f", job_name, gib_billed, gib_billed / 1024 * 6.25) AS line
FROM jobs
ORDER BY job_name;| line |
|---|
| daily_orders 1,536 GiB $9.38 |
| events_backfill 20,480 GiB $125.00 |
Common mistakes
- Using
%sfor numbers.FORMAT('%s', 42)fails withInvalid type for argument 2 to FORMAT; Expected STRING; Got INT64. Use%dfor INT64 or%tfor any type. - Using
%dfor FLOAT64 or NUMERIC. It fails at compile time; use%.0f, or cast to INT64 (which rounds) first. - Expecting NULLs to print as text. Any NULL argument makes the result NULL unless its specifier is
%tor%T; use%tor wrap the value in IFNULL. - Assuming rounding on exact decimals with FLOAT64.
FORMAT('%.2f', 2.675)returns2.67because the binary float is slightly below 2.675;FORMAT('%.2f', NUMERIC '2.675')returns2.68. - Porting PostgreSQL's
format('%s', ...)orto_char. BigQuery's FORMAT is printf-style with typed specifiers, and there is no TO_CHAR; date patterns go through FORMAT_DATE and FORMAT_TIMESTAMP.
FAQ
How do I format a number with commas in BigQuery?
Use the ' flag: FORMAT("%'d", 1234567) returns 1,234,567, and FORMAT("%'.2f", amount) adds two decimals. The separator is always a comma; FORMAT isn't locale-aware.
How do I round to 2 decimal places as a string?
FORMAT('%.2f', value) returns a STRING with exactly two decimals, including trailing zeros. If you need a number, use ROUND(value, 2) instead.
How do I add leading zeros to a number in BigQuery?
FORMAT('%06d', n) pads INT64 values to six digits and handles negative numbers correctly. LPAD(CAST(n AS STRING), 6, '0') works for non-negative values.
What is the difference between %t and %s in FORMAT?
%s accepts only STRING. %t accepts any type and prints a readable form (dates as 2026-11-23, arrays as [1, 2]), and prints NULL for NULL values instead of returning NULL.
Related
Errors you may hit
More string functions
All BigQuery string functions · All functions · BigQuery error messages
