New Built-in Function STRING_AGG() – SQL Server 2017

SQL Server 2017 introduces a set of useful functions like STRING_AGG(), STRING_SPLIT(), TRIM()… and many more. In this tip, I am going to discuss one of the built-in aggregate functions – “STRING_AGG()”.

The function concatenates values from rows as one value with a separator, and the main advantage is it doesn’t add the separator at the end of the value. It means we do not have to use the old XML trick to concatenate values. 

Syntax for the function


Let’s follow the demonstration to understand how you can use it.

  • Create a simple table structure:

  • Insert some random data into the table

  • Let’s apply the STRING_AGG() function to get the comma separated values under a single row;

Here is output of the above query

 To check more about STRING_AGG(),  kindly follow the MSDN link.

Hope, you find the post useful!

Leave a comment

Your email address will not be published. Required fields are marked *