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)
 selecting many rows as a comma delimted column

Author  Topic 

mike123
Master Smack Fu Yak Hacker

1462 Posts

Posted - 2009-11-26 : 17:12:38
Hi,

I have a query I am trying to write, basically its like this.

Select photoID FROM tblPhotos

Its very simple, but instead of it bringing back a bunch of rows, I want it to bring back a column that is the list of photoID's with a comma between them.


For example

Instead of

photoID
1
2
3
4
5

I want to bring back

photoString
1,2,3,4,5



Any thoughts on how I can do this ? I am doing this way because I want the data formatted like this for my web app, to pass into a js function. Also because I want to bring back a better data footprint for my query.


Thanks very much!
mike

Vinnie881
Master Smack Fu Yak Hacker

1231 Posts

Posted - 2009-11-26 : 22:09:50
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=81254


Success is 10% Intelligence, 70% Determination, and 22% Stupidity.
\_/ _/ _/\_/ _/\_/ _/ _/- 881
Go to Top of Page

vikky
Yak Posting Veteran

54 Posts

Posted - 2009-11-27 : 02:11:02
hi,

select stuff((select ','+cast(photoid as varchar(10)) from yourtable for xml path('')),1,1,'')

Thanks,
vikky.
Go to Top of Page
   

- Advertisement -