Getting comma separated string for particular field in sqlserver.

In this example I am showing how to get comma separated string for particular filed in sqlserver. I am using sqlserevr 2008.

Table Data:

Id   Email                     Name
2    TEST@TEST.COM    A
2    TEST@TEST.COM    B
2    TEST@TEST.COM    C
2    TEST@TEST.COM    D
2    TEST@TEST.COM    E
1    TEST@TEST.COM    A
1    TEST@TEST.COM    C
1    TEST@TEST.COM    D
1    TEST@TEST.COM    B
1    TEST@TEST.COM    D
5    TEST@TEST.COM    A
5    TEST@TEST.COM    C
5    TEST@TEST.COM    D
5    TEST@TEST.COM    B
5    TEST@TEST.COM    D

Output:

1    A,B,C,D,D
2    A,B,C,D,E
5    A,B,C,D,D

DECLARE @TABLE TABLE(ID INT, EMAIL VARCHAR(20), NAME VARCHAR(10))

INSERT INTO @TABLE
SELECT 2    ,’TEST@TEST.COM’,    ‘A’ UNION ALL
SELECT 2    ,’TEST@TEST.COM’,    ‘B’ UNION ALL
SELECT 2    ,’TEST@TEST.COM’,    ‘C’ UNION ALL
SELECT 2    ,’TEST@TEST.COM’,    ‘D’ UNION ALL
SELECT 2    ,’TEST@TEST.COM’,    ‘E’ UNION ALL
SELECT 1    ,’TEST@TEST.COM’,    ‘A’ UNION ALL
SELECT 1    ,’TEST@TEST.COM’,    ‘C’ UNION ALL
SELECT 1    ,’TEST@TEST.COM’,    ‘D’ UNION ALL
SELECT 1    ,’TEST@TEST.COM’,    ‘B’ UNION ALL
SELECT 1    ,’TEST@TEST.COM’,    ‘D’ UNION ALL
SELECT 5    ,’TEST@TEST.COM’,    ‘A’ UNION ALL
SELECT 5    ,’TEST@TEST.COM’,    ‘C’ UNION ALL
SELECT 5    ,’TEST@TEST.COM’,    ‘D’ UNION ALL
SELECT 5    ,’TEST@TEST.COM’,    ‘B’ UNION ALL
SELECT 5    ,’TEST@TEST.COM’,    ‘D’

SELECT T2.ID,
STUFF((SELECT ‘,’+T1.NAME FROM @TABLE T1 WHERE T1.ID=T2.ID ORDER BY T1.NAME FOR XML PATH(”)),1,1,”)
FROM @TABLE T2
GROUP BY T2.ID

Advertisements

22 thoughts on “Getting comma separated string for particular field in sqlserver.

  1. I actually wanted to write a small word so as to say thanks to you for all the nice concepts you are posting at this website. My considerable internet research has finally been paid with awesome information to go over with my friends and classmates. I ‘d state that that most of us site visitors actually are undeniably fortunate to be in a notable network with very many perfect professionals with helpful tactics. I feel very much fortunate to have come across your web pages and look forward to tons of more amazing times reading here. Thank you once more for a lot of things.

    Like

  2. I have learn several good stuff here. Certainly value bookmarking for
    revisiting. I surprise how a lot effort you put to make such a excellent informative site.

    Like

  3. As I internet internet site possessor I believe the content material matter here is rattling amazing , appreciate it for your efforts. You must maintain it up forever! Very good Luck.

    Like

  4. 421814 84343I discovered your weblog internet site internet internet site on the internet and appearance some of your early posts. Continue to maintain inside the wonderful operate. I just now additional increase your Rss to my MSN News Reader. Seeking toward reading far far more from you obtaining out at a later date! 285602

    Like

  5. An intriguing discussion is worth comment. I feel which you really should write far more on this subject, it may possibly not be a taboo topic but normally men and women are not enough to speak on such topics. Towards the next. Cheers

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s