0

I am trying to convert the following query into SQL Server from MySQL:

SELECT name, GROUP_CONCAT(DISTINCT language)
FROM table GROUP BY name

It seems like STRING_AGG() is the related function name in SQL Server, but I haven't yet been able to write the correct query.

How would this be properly be done?

Update: I believe I may not have access to that function (pre 2017). How would I then do something like:

SELECT name, (select language from table ??)
FROM table GROUP BY name
Dale K
  • 25,246
  • 15
  • 42
  • 71
samuelbrody1249
  • 4,379
  • 1
  • 15
  • 58

1 Answers1

1

Can you please try this.

DECLARE @Table TABLE
(
    ID INT IDENTITY(1,1) PRIMARY KEY,
    name VARCHAR(50),
    language VARCHAR(50)
)


INSERT INTO @Table VALUES('A','en'),('b','nl'),('c','de'),('A','nl')

SELECT
      name
     ,STUFF((
          SELECT ',' + T.language
          FROM @Table T
          WHERE T.name = tb.name 
          FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 1, '') AS [lang]
FROM @Table tb
GROUP BY tb.name

Output be like

name       lang
---------- -----------------------
A          en,nl
b          nl
c          de
jishan siddique
  • 1,848
  • 2
  • 12
  • 23