Combine Multiple Rows into One String in SQL Server | STRING_AGG Explained

Published: 26 February 2026
on channel: Data Skills Daily
12
1

This lesson covers how to use the T-SQL STRING_AGG function to concatenate (combine) values from multiple rows into a single string.

What is STRING_AGG?

STRING_AGG is an aggregate function (like SUM or AVG) that concatenates string values from multiple rows into a single string. It takes two required parameters:

Basic syntax:
STRING_AGG (expression, separator)

Where:
expression: The value to concatenate (usually a column)
separator: The string to insert between each value

Full query examples:
Basic concatenation:

SELECT
CategoryID,
STRING_AGG(ProductName, '; ') AS Products
FROM Products
GROUP BY CategoryID;


With sorting:
SELECT
CategoryID,
STRING_AGG(ProductName, '; ')
WITHIN GROUP (ORDER BY ProductName) AS Products
FROM Products
GROUP BY CategoryID;


Important notes:

STRING_AGG ignores NULL values automatically
The function was introduced in SQL Server 2017
It's also available in Azure SQL Database

The result type is NVARCHAR or VARCHAR depending on input

Common use cases:
Creating comma-separated lists for reports
Building email recipient lists
Generating JSON-like strings
Formatting output for export files


#SQLServer #TSQL #DatabaseAdministration #DataAnalytics #SQLTutorial #CodingTips #LearnSQL #TechEducation #DatabaseDevelopment #SQLQueries


On this page of the site you can watch the video online Combine Multiple Rows into One String in SQL Server | STRING_AGG Explained with a duration of hours minute second in good quality, which was uploaded by the user Data Skills Daily 26 February 2026, share the link with friends and acquaintances, this video has already been watched 12 times on youtube and it was liked by 1 viewers. Enjoy your viewing!