group_concat sql server function code example
Example 1: group_concat sql server
STRING_AGG ( expression, separator ) [ <order_clause> ]
<order_clause> ::=
WITHIN GROUP ( ORDER BY <order_by_expression_list> [ ASC | DESC ] )
SELECT STRING_AGG(Genre, ',') AS Result
FROM Genres;
Result:
Result
Rock,Jazz,Country,Pop,Blues,Hip Hop,Rap,Punk
Example 2: sql server group by concatenate
CREATE TABLE
INSERT INTO
INSERT INTO
INSERT INTO
SELECT
[ID],
STUFF((
SELECT ', ' + [Name] + ':' + CAST([Value] AS VARCHAR(MAX))
FROM
WHERE (ID = Results.ID)
FOR XML PATH(''),TYPE).value('(./text())[1]','VARCHAR(MAX)')
,1,2,'') AS NameValues
FROM
GROUP BY ID
DROP TABLE