SQL SERVER – Concatenates rows fields into a single string with STRING_AGG()

 

The STRING_AGG() is an aggregate function that concatenates rows of strings into a single string, separated by a specified separator. It does not add the separator at the end of the result string.

This example uses the STRING_AGG() function to generate lists of emails of customers by the city:

SELECT
    city, 
    STRING_AGG(email,';') email_list
FROM
    sales.customers
GROUP BY
    city;

This is the result

Comments