String_Agg() Function in SQL Server | how to get comma separated values in sql

preview_player
Показать описание

Sccript:-
CREATE TABLE StringAgg_Tbl(id int,name char(3))
INSERT INTO StringAgg_Tbl VALUES (1,'CD')
,(1,'AB')
,(2,'LM')
,(3,'BC')
,(3,'GH')
,(4,'KJ')
,(3,'AB')


SELECT id,
STRING_AGG (name, ',') AS data
FROM StringAgg_Tbl
GROUP BY id;

SELECT id,
STRING_AGG (name, ',') WITHIN GROUP (ORDER BY name ASC) AS data
FROM StringAgg_Tbl
GROUP BY id;

DROP TABLE StringAgg_Tbl

For SQL Quiz-

Find Us On FaceBook-
Рекомендации по теме
Комментарии
Автор

Hats 👑⛑👒🎩off to your explanations. Thanks

rushiindra
Автор

on existing column how to insert delimiter eg: Full name 'Raviverma' output 'Ravi-verma' can pls provide solution

rvtjpmr
Автор

Hello sir, any chances of complete project explanation of sql server. How does it work in real world.

Thank you!!

sushanthbollaboina
Автор

Hello sir, i have one string
I already separated it using create function which is similar like string_split because i have old version of server,
So sir, i have table which contains 8 columns col1, col2, col, 3
I have to insert the seprated string values into this columns(8 columns are their)
I am seprated it but they are form rows
My
Name
Is
Tushar
Like that i need to insert into columns or horizontal way.

Can you help me out???

tusharjadhav
Автор

Its not working on my sql server verion 17.2

Mistysoil
Автор

Sir...suppose Id 1 having CD CD CD 3 times how to make unique values in String Agg function output???

RaviKumar-pnuw
Автор

How to get comma separated output as table

naveenreddym
Автор

getting error string_agg does not exist

shammi
Автор

please mentioned the server version also.

landchennai
welcome to shbcf.ru