365
Views
6
Comments
STRING_AGG not work in SQL
Question

Anybody can help?

String_AGG is not workable?



2018-10-29 08-31-03
João Marques
 
MVP

Hi Jessica,


To aggregate strings, you should use the STUFF command (see how here).


ON your example, it would be something like this

SELECT
    STUFF((
        SELECT ',' + {Group}.[Description]
        FROM
            {Group_User}
        LEFT JOIN {Group} R ON {Group_User}.[Group_Id] = {Group}.[Id]
        FOR XML PATH('')), 1, 1, '')


Cheers,

João Marques

2022-03-10 08-26-10
Cristian Angel Puma Villalva

thanks joao 

UserImage.jpg
Jessica Lee

Thank Joao.


2020-11-10 23-58-16
Raphael Ranieri
 
MVP

Jessica, in addition what João said, the reason string_agg is not working may be the version of your DB.


It was added in SQL Server 2017 (14.x) and later or SQL Azure. See microsoft docs about it


The stuff approach João commented is an alternative for other SQL versions

UserImage.jpg
Jessica Lee

Thank Raphael.

May I know how to check the version of sql server in the cloud platform?

2020-11-10 23-58-16
Raphael Ranieri
 
MVP

Jessica,

Try to create an Advanced SQL and TEST the following query:

EXECUTE('SELECT @@VERSION');


This way:


It should show you the version in test output:




Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.