
) clause is part of the LISTAGG function syntax so there's no way to concat strings without specifying the order.

LISTAGG(child_name, ',') WITHIN GROUP (ORDER BY child_name) In Oracle from version 11g you can use the LISTAGG function. SELECT parent_name, string_agg(child_name, ',') Prior to version 8.4, you would have had to define the function ARRAY_AGG before using it:įrom version 9.0 on, you can use the string_agg function: If you want to order the names in each group:ĪRRAY_TO_STRING(ARRAY_AGG(child_name ORDER BY child_name ASC), ',') The function ARRAY_AGG() returns an array of elements in the group, the function ARRAY_TO_STRING() concatenates the elemens in the array using the given separator. SELECT parent_name, ARRAY_TO_STRING(ARRAY_AGG(child_name), ',') In PostgreSQL you use ARRAY_TO_STRING() and ARRAY_AGG() functions: Let's take a look at how you can aggregate strings in a group in other databases. In most other databases, there is no GROUP_CONCAT() function. Group_concat(child_name ORDER BY child_name asc separator ' ') To make sure the names of the children in each string are alphabetically ordered and to use semicolon " " as a separator, use this query: | parent_name | group_concat(child_name) | SELECT parent_name, group_concat(child_name) STRING_AGG(al.AlbumName, ', ') WITHIN GROUP (ORDER BY al.To get the names of children of each person as a comma-separated string, you use the GROUP_CONCAT() function as follows: When using this clause, you specify the order with ORDER BY followed by either ASC (for ascending) or DESC (for descending). This is done with the WITHIN GROUP clause. You can use an order clause to order the results within the concatenated group. Tom Jones Long Lost Suitcase, Praise and Blame, Along Came Jones Michael Learns to Rock Blue Night, Eternity, Scandinavia Iron Maiden Powerslave, Somewhere in Time, Piece of Mind, Killers, No Prayer for the Dying So a regular query joining both tables might look something like this: USE Music Īs you can see, if an artist has more than one album, the artist’s name is listed multiple times – once for each album.īut we can use STRING_AGG() to change this so that we list each artist only once, followed by a comma-separated list of albums they’ve released: USE Music Īllan Holdsworth All Night Wrong, The Sixteen Men of Tainĭevin Townsend Ziltoid the Omniscient, Casualties of Cool, Epicloud For every artist, there could be many albums. There is a one to many relationship between these tables. To demonstrate this, imagine a database with two tables Artists and Albums. For example, you might want a list of albums grouped by artist. You can also use the STRING_AGG() function when grouping your result set. However, if we use the ISNULL() function to provide a placeholder for any null values, we get this: SELECT STRING_AGG(ISNULL(TaskCode, 'N/A'), ', ') If we run this through the STRING_AGG() function, we get this: SELECT STRING_AGG(TaskCode, ', ')

We can see that there are three null values within the result set. Doing this ensures that you still get a result when a row contains a null value.įor example, consider the following query and result set: SELECT TaskCode If this isn’t suitable, you can provide a value for null values by using the ISNULL() function and passing in the value you’d like to use whenever a null value is encountered. If your result set contains null values, those values are ignored and a corresponding separator is not added. Result: 1) Feed cats 2) Water dog 3) Feed garden 4) Paint carpet 5) Clean roof 6) Feed cats We could also use the CONCAT() function to combine two fields together, separated by their own separator.Įxample: SELECT STRING_AGG(CONCAT(TaskId, ') ', TaskName), ' ') It can be separated by any expression of NVARCHAR or VARCHAR type, and it can be a literal or a variable. Of course, it doesn’t necessarily need to be separated by a comma.

Result: Feed cats, Water dog, Feed garden, Paint carpet, Clean roof, Feed cats Like this: SELECT STRING_AGG(TaskName, ', ') So we could take the above data, and use the STRING_AGG() function to list all the task names in one big comma separated list. This article provides examples that demonstrate the T-SQL STRING_AGG() function.įirst, here’s some sample data. This works in much the same way to MySQL’s GROUP_CONCAT() function. Transact-SQL now has the STRING_AGG() function, which concatenates the values of string expressions and places separator values between them. While it’s true that you could achieve this same effect prior to SQL Server 2017, it was a bit fiddly. This means you can have your result set appear as a comma-separated list, a space-separated list, or whatever separator you choose to use. Starting with SQL Server 2017, you can now make your query results appear as a list.
