Click here to Skip to main content
15,893,161 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
no name

1800 t1
1800 t2
1800 t3
1800 t4
1800 t5
1900 t6
1900 t7
1900 t8
2000 t100

I have data ( like above mentioned ) in table
what i want to do is
i want to show it to like this ( in comma separated value )

no
1800 t1,t1,t3,t4,t5
1900 t6,t7,t8
2000 t100

what the query to get result l like this

note : i have data in hundreds or even thousands. its just a sampple to clear things
Posted
Comments
Reza Alipour Fard 16-Jul-13 1:38am    
you data is in one column or two columns?
1900 t8 is a column or
1900 is a column and t8 is a column?

1 solution

Hi, try this: (Note that Table_1 is your table name.)

SQL
SELECT
DISTINCT r.no,
       STUFF((SELECT DISTINCT ','+ a.name
              FROM Table_1 a
              WHERE r.no = a.no
              FOR XML PATH(''), TYPE).value('.','VARCHAR(max)'), 1, 1, '')
FROM Table_1 r


I tried it by myself, and it's working.
 
Share this answer
 
v2
Comments
Reza Alipour Fard 16-Jul-13 2:03am    
I vote 5 out of 5. Your solution is very short and correct. Thanks
berrymaria 16-Jul-13 2:06am    
thank you! :))
zenspace 16-Jul-13 2:07am    
Good answer! :D
berrymaria 16-Jul-13 2:08am    
thanks! :]
Raja Sekhar S 16-Jul-13 4:02am    
Nice one... +5..!

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