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 2000 Forums
 SQL Server Development (2000)
 Aggregate text into one column

Author  Topic 

stephe40
Posting Yak Master

218 Posts

Posted - 2006-08-18 : 10:58:32
I sure I am not the first person to ask this, for that I apologize, but I can not find anything in the search.


Given the following data:

key data
1 A
1 B
1 C
2 A
2 C

What is the best way to get the data back like this:

key data
1 A, B, C
2 A, C


All the ideas I am coming up with require hard coding the key id's somewhere.

- Eric

stephe40
Posting Yak Master

218 Posts

Posted - 2006-08-18 : 11:01:03
I just realized I could use a UDF that given an ID would return the right string of stuff. Is there a built in way?

- Eric
Go to Top of Page

nr
SQLTeam MVY

12543 Posts

Posted - 2006-08-18 : 11:31:56
UDF is the simplest - could probably use a CTE in v2005.
the UDF would be

declare @s varchar(1000)
select @s = coalesce(@s + ',',data)
from tbl
where key = @key



==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-08-18 : 11:50:23
Also refer this
http://sqljunkies.com/WebLog/amachanic/archive/2004/11/10/5065.aspx?Pending=true


Madhivanan

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

- Advertisement -