STRING_AGG()
STRING_AGG concatenates the values of string expressions and places separator values between them.
Signature
STRING_AGG ( expression , separator ) [ <order_clause> ]
<order_clause> ::=
WITHIN GROUP ( ORDER BY <order_by_expression_list> [ ASC | DESC ] )Parameters
- expression — An expression of any type. Expressions are converted to nvarchar or varchar types during concatenation. Non-string types are converted to nvarchar type.
- separator — An expression of nvarchar or varchar type that is used as separator for concatenated strings. It can be literal or variable.
Examples
SELECT STRING_AGG(product, ', ') WITHIN GROUP (ORDER BY product) AS products FROM dbo.orders;products
----------------------------------------------------------------
cable, keyboard, keyboard, monitor, monitor, mouseRun against SQL Server 16.0.4265.3 on 2026-09-13. This is the output we got, not the documentation’s.
Available on
- SQL Server 2017 and later
- Azure SQL Database
- Azure SQL Managed Instance
- Azure Synapse Analytics
- Microsoft Fabric warehouse
- Fabric SQL database
Signature and description adapted from the Microsoft SQL documentation, used under the CC BY 4.0. SQL Server and Azure are trademarks of the Microsoft group of companies. This page is not affiliated with Microsoft.