dataengref migrate

Home / SQL Server

SQL Server STRING_AGG in Snowflake, Databricks and BigQuery

BigQuery and Databricks both have STRING_AGG; Snowflake calls it LISTAGG. Ordering syntax differs: SQL Server and Snowflake use WITHIN GROUP (ORDER BY ...), BigQuery puts ORDER BY inside the parentheses.

SQL Server function: STRING_AGG(expression, separator) [WITHIN GROUP (ORDER BY ...)] · SQL Server docs

Equivalents

TargetSupportSyntaxNotesSource
SnowflakeDifferent nameLISTAGG([DISTINCT] expr [, delimiter]) [WITHIN GROUP (ORDER BY ...)]Ignores NULL values. The default delimiter is an empty string.Snowflake docs
DatabricksSame namestring_agg([DISTINCT] expr [, delimiter]) [WITHIN GROUP (ORDER BY ...)]Databricks SQL and Databricks Runtime 16.4 and later; alias of listagg. On older runtimes use array_join(collect_list(expr), delimiter), which does not guarantee order.Databricks docs
BigQuerySame nameSTRING_AGG([DISTINCT] expr [, delimiter] [ORDER BY ...] [LIMIT n])ORDER BY goes inside the parentheses. The default delimiter is a comma.BigQuery docs

Behavior differences to check

Snowflake

Databricks

BigQuery

Examples

Products of each order, ordered by name

SQL Server
SELECT order_id, STRING_AGG(product, ', ') WITHIN GROUP (ORDER BY product) AS products FROM order_items GROUP BY order_id;
Snowflake
SELECT order_id, LISTAGG(product, ', ') WITHIN GROUP (ORDER BY product) AS products FROM order_items GROUP BY order_id;
Databricks
SELECT order_id, string_agg(product, ', ') WITHIN GROUP (ORDER BY product) AS products FROM order_items GROUP BY order_id;
BigQuery
SELECT order_id, STRING_AGG(product, ', ' ORDER BY product) AS products FROM order_items GROUP BY order_id;

Other SQL Server functions

Last checked: · Paste a whole query into the SQL checker to find every function that needs a change.