STRING_AGG
string aggregation
What changes
- Input
- string expression over each group's rows, separator, optional WITHIN GROUP order
- Output or change
- one concatenated string per group
- Return type
- Return type depends on first argument (expression). If input argument is string type (nvarchar, varchar), the result type is the same as the input type. The following table lists automatic conversions
Syntax at a glance
STRING_AGG ( expression , separator ) [ <order_clause> ]
Work example
Combine grouped values into one delimited display field.
Add WITHIN GROUP ordering when the output order matters and choose a result type with enough capacity.
Check your work STRING_AGG ignores null inputs and does not append a trailing separator.
Practice SQL
Open in SQL FlowSELECT STRING_AGG(code, ', ') WITHIN GROUP (ORDER BY code) AS codes FROM (VALUES ('A1'), ('B2')) AS v(code);
Open Microsoft Learn for full syntax, permissions, and details for your version.