Possible Duplicate:
Simulating group_concat MySQL function in MS SQL Server 2005?
I have a query that needs to return a set of fields, along with a list of data returned as a single character-separated string.
Say I have 2 tables
tblPeople
NamePK Name
-----------------
1 Fred
2 Tom
tblNotes
NameFK Note
------------------------
1 Likes to talk
1 Types fast
2 Joined the internet
Then my answer would look like this (if I separated with a ‘+’)
NamePK Name Notes
-------------------------------------------
1 Fred Likes to talk+Types fast
2 Tom Joined the internet
I believe I have to use the COALESCE function. I have used it in the past to create comma-delimited values, but I am unsure on how to do this in a select. I’m using MSSQL.
Any ideas or approaches?
suggestions:
Sql COALESCE entire rows
Use
STUFFandFOR XML PATHto do the concatenation. See this question for an example. There are improvements that can be made, such as eliminating extra delimiters, if you search around for other questions involvingSTUFF.