Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 comma separated list from the rows of a table?

Author  Topic 

00kevin
Yak Posting Veteran

78 Posts

Posted - 2008-11-07 : 11:44:27
Can anyone here point me to a stored procedure or function that will create a comma separated list out of the rows of a table?


For example

I have a table called Car and a 1-N relationship to another table called Parts. I want to return a single result set with all the rows in the parts table that belong to Car as a comma separated list. The comma separated list should be in a single field.


select
car_id,
car_name,
some_function(car_id) as car_parts_list , -- some function that I can pass the car_id
from car



visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-11-07 : 11:50:35
[code]select
car_id,
car_name,
left(pl.partslist,len(pl.partslist)-1)
from car c
cross apply (select partsfield + ','
from parts
where car_id=c.car_id
for xml path(''))pl(partslist)[/code]
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-11-10 : 02:15:41
or
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=81254

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -