Click here to Skip to main content
15,919,422 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi All,

I have the below requirement.
It is a two column table when the records will be as below:

ID Name
-- ------
1 Mathi
1 Gandhi
1 Nathan
2 Aashiq
2 Babu
3 Manmohan
3 Kumar
3 Bestha

But I want these records like this:

ID Name
-- ------
1 Mathi,Gandhi,Nathan
2 Aashiq,Babu
3 Manmohan,Kumar,Bestha

Thanks & Regards,
Mathi.
Posted
Comments
Richard Deeming 14-Oct-14 8:48am    
This blog post[^] covers quite a few different options.

1 solution

SQL
Try this,

SELECT DISTINCT ID, STUFF((SELECT ','+ CONVERT(VARCHAR(30), name)
             FROM tablename AS S2
             WHERE S1.ID = S2.ID
             FOR XML PATH('')), 1, 1, '') AS Name
FROM tablename AS S1
 
Share this answer
 
Comments
aarif moh shaikh 15-Oct-14 0:51am    
Well Done .. Gopu Raj
Rajesh waran 15-Oct-14 0:57am    
Thank you,Aarif moh shaikh
Mathi2code 15-Oct-14 6:45am    
Cool Gopu raj. Thanks
Rajesh waran 15-Oct-14 6:59am    
U welcome

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900