Back
Consider a database table holding names, with three rows:
PeterPaulMary
Peter
Paul
Mary
Is there an easy way to turn this into a single string of Peter, Paul, Mary?
In SQL 2005, if you are trying to join two tables with one-to-many relationships use XML PATH method to concatenate the rows:
SELECT DISTINCT EM2.DepartmentID, SUBSTRING((SELECT ','+EM1.EmployeeName AS [text()] FROM dbo.Empolyees EM1 WHERE EM1.EmployeeID = EM2.EmployeeID ORDER BY EM1.DepartmentID FOR XML PATH ('') ), 2, 1000) [Employees]FROM dbo.Employees EM2
SELECT STRING_AGG( ISNULL(EmployeeName, ' '), ',') As Countries From FROM dbo.Employees EM
31k questions
32.8k answers
501 comments
693 users