growled on Tuesday, May 24, 2005 1:55:59 PM (Pacific Standard Time, UTC-08:00)
barked at code [t-sql]

It's been quite awhile since I posted a how2 entry, which are consistently my biggest source of traffic from searches...so here goes another...

A few weeks ago I was looking to create a table in an ASP.NET page with one of the columns containing the result set of multiple rows from one of the SQL tables.  Basically, I wanted to concatenate multiple rows returned from a T-SQL query into one long string and dump that into the ASP.NET dataset.  Make sense?  Probably not so here's an example:

SqlTable1 contains two columns, Team and Person.  A person can be a member of multiple teams so a select * from SqlTable1 might look like this:

Team - Person
A - Mel
A - Danny
A - Rene
B - Mel
B - Danny
B - Joe
C - Rene
C - Chris

And I want to return this in my ASP.NET dataset:

Team - Members
A - Mel, Danny, Rene
B - Mel, Danny, Joe
C - Rene, Chris

Hopefully that's clear now.  :)  You would think that a powerful database application like SQL 2000 would be readily capable of something like this and it is, but it wasn't obvious to me (or most of the internet) how to do it.

I did several searches via MSN Search and Google and finally ended up implementing this solution using a temporary table and a staging table in SQL to hold my results.  Very ugly and hackish, but it works.

Then a few days later Emery (my cohort in crime at work) and I were looking over my code when he shared the 'easy' way with me (he had just learned it himself a few weeks before).

DECLARE @list VARCHAR(8000)
SELECT @list = ISNULL(@list,") + ',' + Person
FROM SqlTable1
WHERE Team = 'A'

If you perform a SELECT @list it will return "Mel, Danny, Rene."  You can even wrap this up in a SQL function if you like.

Hope that helps!

~tod

Comments Comments [7]  

Related posts:
Happy New Year (and oh, by the way, the sh*t's broke again)
how2: Add a default value to an existing column using T-SQL
Tracked by:
"http://www.google.com/search?q=nktjoysz" (http://www.google.com/search?q=nktjoy... [Pingback]
"http://www.crt.state.la.us/ltgovernor/media_view.aspx?id=277" (http://www.crt.s... [Pingback]
"http://ertr.tamu.edu/conferenceabstracts.cfm?abstractid=3006" (http://ertr.tamu... [Pingback]
"http://www.collegetennisonline.com/view/news.aspx?nwId=32388" (http://www.colle... [Pingback]
"http://www.internshipprograms.com/CompanyDetail.asp?CompanyID=34530" (http://ww... [Pingback]
"http://www.fastpitchnetworking.com/pressrelease.cfm?PRID=20579" (http://www.fas... [Pingback]
"http://www.pharmiweb.com/PressReleases/pressrel.asp?ROW_ID=3247" (http://www.ph... [Pingback]
"http://www.ideamarketers.com/library/profile.cfm?writerid=42881" (http://www.id... [Pingback]
"http://www.winthrop.org/newsroom/archives/release.cfm?id=366" (http://www.winth... [Pingback]
"http://www.google.com/search?q=beaooytn" (http://www.google.com/search?q=beaooy... [Pingback]
"http://www.google.com/search?q=ncdpdjow" (http://www.google.com/search?q=ncdpdj... [Pingback]
"http://www.google.com/search?q=vocihxnh" (http://www.google.com/search?q=vocihx... [Pingback]
"http://www.engcen.com/EmpProfile.asp?EmployerID=5954" (http://www.engcen.com/Em... [Pingback]
"http://clutchfans.net/feature.cfm?FeatureID=168" (http://clutchfans.net/feature... [Pingback]
"http://www.engcen.com/EmpProfile.asp?EmployerID=5987" (http://www.engcen.com/Em... [Pingback]
"http://www.csp.state.co.us/press_releases.cfm?Article_ID=302" (http://www.csp.s... [Pingback]
"http://clutchfans.net/feature.cfm?FeatureID=178" (http://clutchfans.net/feature... [Pingback]
"http://www.southafrica.net/satourism/media/newsreleasedetail.cfm?NewsReleaseID=... [Pingback]
"http://www.landandfarm.com/lf/news/display_article.asp?cid=739" (http://www.lan... [Pingback]
"http://www.csp.state.co.us/press_releases.cfm?Article_ID=323" (http://www.csp.s... [Pingback]
"http://www.seriea.tv/NewsDetails.asp?articolo=990" (http://www.seriea.tv/NewsDe... [Pingback]
"http://www.csp.state.co.us/press_releases.cfm?Article_ID=326" (http://www.csp.s... [Pingback]
"http://clutchfans.net/feature.cfm?FeatureID=193" (http://clutchfans.net/feature... [Pingback]
"http://www.southafrica.net/satourism/media/newsreleasedetail.cfm?NewsReleaseID=... [Pingback]
"http://www.townhall.state.va.us/L/ViewChapter.cfm?chapterid=2552" (http://www.t... [Pingback]
"http://www.edinburgh.gov.uk/environmentaldirectory/orgDetail.jsp?id=155" (http:... [Pingback]
"http://www.destinationcrm.com/articles/default.asp?ArticleID=7581" (http://www.... [Pingback]
"http://www.destinationcrm.com/articles/default.asp?ArticleID=7569" (http://www.... [Pingback]
"http://www.anaheimoc.org/press/pressNews.asp?pid=124" (http://www.anaheimoc.org... [Pingback]
"http://www.destinationcrm.com/articles/default.asp?ArticleID=7559" (http://www.... [Pingback]
"http://www.epiphone.com/news.asp?NewsID=1105" (http://www.epiphone.com/news.asp... [Pingback]
"http://www.epiphone.com/news.asp?NewsID=1048" (http://www.epiphone.com/news.asp... [Pingback]
"http://www.epiphone.com/news.asp?NewsID=1054" (http://www.epiphone.com/news.asp... [Pingback]
"http://www.destinationcrm.com/articles/default.asp?ArticleID=7561" (http://www.... [Pingback]
"http://www.epiphone.com/news.asp?NewsID=1096" (http://www.epiphone.com/news.asp... [Pingback]
"http://www.telepacific.com/aboutTelePacific/press/press.asp?id=2072" (http://ww... [Pingback]
"http://www.myanmarmp3.net/readnews.aspx?NewsID=214" (http://www.myanmarmp3.net/... [Pingback]
"http://www.myanmarmp3.net/readnews.aspx?NewsID=209" (http://www.myanmarmp3.net/... [Pingback]
"http://www.myanmarmp3.net/readnews.aspx?NewsID=226" (http://www.myanmarmp3.net/... [Pingback]
"http://lakeline.lib.fl.us/news_and_events/calendar/lclseventdetail.aspx?IDEvent... [Pingback]
"http://www.collegetennisonline.com/view/news.aspx?nwId=32645" (http://www.colle... [Pingback]
"http://www.telepacific.com/aboutTelePacific/press/press.asp?id=2089" (http://ww... [Pingback]
"http://www.myanmarmp3.net/readnews.aspx?NewsID=219" (http://www.myanmarmp3.net/... [Pingback]
"http://asp1.umbc.edu/newmedia/studio/stream/qtdetail.cfm?recordID=579" (http://... [Pingback]
"http://www.telepacific.com/aboutTelePacific/press/press.asp?id=2093" (http://ww... [Pingback]
"http://www.telepacific.com/aboutTelePacific/press/press.asp?id=2084" (http://ww... [Pingback]
"http://www.collegetennisonline.com/view/news.aspx?nwId=32654" (http://www.colle... [Pingback]
"http://lakeline.lib.fl.us/news_and_events/calendar/lclseventdetail.aspx?IDEvent... [Pingback]
"http://www.collegetennisonline.com/view/news.aspx?nwId=32633" (http://www.colle... [Pingback]
"http://lakeline.lib.fl.us/news_and_events/calendar/lclseventdetail.aspx?IDEvent... [Pingback]
"http://tubepornoss.com" (http://tubepornoss.com) [Pingback]
"http://realvideopornoo.com" (http://realvideopornoo.com) [Pingback]
"http://yesihavemoneyy.com" (http://yesihavemoneyy.com) [Pingback]
Wednesday, July 26, 2006 4:56:17 AM (Pacific Standard Time, UTC-08:00)
Fantastic!!

For years now I've been wondering how to concatenate rows to a string, and have never found the answer.

This is great, nice one!
Jeff
Wednesday, July 26, 2006 8:49:30 AM (Pacific Standard Time, UTC-08:00)
Jeff- Glad it helped!
Wednesday, August 02, 2006 7:36:10 AM (Pacific Standard Time, UTC-08:00)
Thank you sir - you are a friend and a scholar.
Tuesday, August 08, 2006 11:00:54 AM (Pacific Standard Time, UTC-08:00)
Jeremy- My pleasure!
Monday, April 30, 2007 11:01:15 AM (Pacific Standard Time, UTC-08:00)
WoW... I was searching that kind of solution for years! I always managed to do something else since it was not truely needed but it surely simplify some reporting!!!

It is SO SIMPLE that it had proven to be tough to find. I was looking too far!!!

Now that I know the answer I wonder why I couldn't think of it myself... Maybe you're smarter than I am !!! he he...

J-B
Monday, April 30, 2007 1:34:25 PM (Pacific Standard Time, UTC-08:00)
J-B- Nope, not smarter...just had different learning experiences. :)
Tuesday, July 17, 2007 8:21:54 AM (Pacific Standard Time, UTC-08:00)
Amazingly brilliant, yet so simple. Saved potentially hours of trial and error. Thank you very much!!
tom
Comments are closed.