SQL Server - Select multiple row values in one column (CSV)

SELECT ClassName, (SELECT SUBSTRING((SELECT ',' + StudentName FROM Students WHERE ClassId = Classes.Id FOR XML PATH('')),2,200000)) as StudentNames FROM Classes WHERE ClassId = 1
Before :
Class 1 | Student 1
Class 1 | Student 2
Class 1 | Student 3
After :
Class 1 | Student1, Student 2, Student 3

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.