BigQuery · Array function
BigQuery ARRAY_TO_STRING
ARRAY_TO_STRING(arr, delimiter[, null_text]) joins the elements of a STRING or BYTES array into one string. NULL elements are skipped unless you pass null_text. Use it to turn tags, page paths or ID lists into readable text.
ARRAY_TO_STRING concatenates elements in array order with the delimiter between them, which makes it the inverse of SPLIT. The array must be ARRAY<STRING> or ARRAY<BYTES>, and the delimiter must have the same type. Numbers, dates and other types fail with No matching signature for function ARRAY_TO_STRING until you cast each element to STRING.
NULL elements are dropped together with their delimiter, so ['US', NULL, 'DE'] joined with '|' gives US|DE. Pass a third argument to print a placeholder instead; an empty string keeps the empty slot (US||DE). A NULL array, a NULL delimiter or a NULL null_text returns NULL, while an empty array returns an empty string.
ARRAY_TO_STRING works on one array per row. To join values from many rows, aggregate with STRING_AGG directly, or build an ordered array with ARRAY_AGG and pass it here.
Syntax
ARRAY_TO_STRING(array_expression, delimiter[, null_text])Arguments and return value
| Argument | Description |
|---|---|
array_expression | An ARRAY<STRING> or ARRAY<BYTES>. |
delimiter | Text placed between elements. Same type as the elements; required. |
null_text | Optional. Replaces each NULL element. Without it, NULL elements and their delimiters are left out. |
Returns: STRING for a STRING array, BYTES for a BYTES array. NULL if the array, the delimiter or null_text is NULL; an empty string for an empty array.
Examples
Join tags into a list
SELECT ARRAY_TO_STRING(['bigquery', 'sql', 'arrays'], ', ') AS tag_list;| tag_list |
|---|
| bigquery, sql, arrays |
NULL elements: skipped, replaced or kept as empty
SELECT
ARRAY_TO_STRING(['US', NULL, 'DE'], '|') AS skipped,
ARRAY_TO_STRING(['US', NULL, 'DE'], '|', 'unknown') AS replaced,
ARRAY_TO_STRING(['US', NULL, 'DE'], '|', '') AS empty_slot;| skipped | replaced | empty_slot |
|---|---|---|
| US|DE | US|unknown|DE | US||DE |
Join numbers by casting each element
ARRAY_TO_STRING rejects ARRAY<INT64>. The ARRAY subquery casts each ID and keeps the original order with WITH OFFSET.
WITH invoices AS (
SELECT 'INV-7' AS invoice_id, [3104, 3105, 3190] AS order_ids
)
SELECT
invoice_id,
ARRAY_TO_STRING(
ARRAY(SELECT CAST(id AS STRING) FROM UNNEST(order_ids) AS id WITH OFFSET AS pos ORDER BY pos),
','
) AS order_id_list
FROM invoices;| invoice_id | order_id_list |
|---|---|
| INV-7 | 3104,3105,3190 |
Page path per session
WITH pageviews AS (
SELECT 's1' AS session_id, 1 AS hit_number, '/home' AS page UNION ALL
SELECT 's1', 2, '/pricing' UNION ALL
SELECT 's1', 3, '/signup' UNION ALL
SELECT 's2', 1, '/blog'
)
SELECT
session_id,
ARRAY_TO_STRING(ARRAY_AGG(page ORDER BY hit_number), ' > ') AS path
FROM pageviews
GROUP BY session_id
ORDER BY session_id;| session_id | path |
|---|---|
| s1 | /home > /pricing > /signup |
| s2 | /blog |
Common mistakes
- Passing ARRAY<INT64> or ARRAY<DATE>. Only STRING and BYTES arrays are accepted; cast the elements first, as in the invoice example.
- Losing NULLs without noticing.
['a', NULL, 'b']becomesa,b, so the number of items in the text no longer matches the array length. Pass null_text when positions matter. - Expecting a NULL array to give an empty string. It returns NULL, while an empty array returns
''. UseIFNULL(ARRAY_TO_STRING(arr, ','), '')for text exports. - Building the array with ARRAY_AGG without ORDER BY. The joined text then comes out in arbitrary order; write
ARRAY_AGG(x ORDER BY ...)orSTRING_AGG(x, ',' ORDER BY ...).
FAQ
How do I convert an array to a comma-separated string in BigQuery?
ARRAY_TO_STRING(arr, ','). For an array of numbers or dates, cast the elements to STRING first, as in the invoice example.
What is the difference between ARRAY_TO_STRING and STRING_AGG?
ARRAY_TO_STRING joins the elements of an array that already exists in a row. STRING_AGG is an aggregate that joins values from many rows, with its own ORDER BY and LIMIT.
How do I turn a delimited string back into an array?
Use SPLIT: SPLIT('a,b,c', ',') returns [a, b, c].
Related
Errors you may hit
More array functions
All BigQuery array functions · All functions · BigQuery error messages
