Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR SQL

concatenate text from multiple rows into a single text stringin SQL Server

SELECT Main.SubjectID,
       LEFT(Main.Students,Len(Main.Students)-1) As "Students"
FROM
    (
        SELECT DISTINCT ST2.SubjectID, 
            (
                SELECT ST1.StudentName + ',' AS [text()]
                FROM dbo.Students ST1
                WHERE ST1.SubjectID = ST2.SubjectID
                ORDER BY ST1.SubjectID
                FOR XML PATH (''), TYPE
            ).value('text()[1]','nvarchar(max)') [Students]
        FROM dbo.Students ST2
    ) [Main]
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #concatenate #text #multiple #rows #single #text #stringin #SQL #Server
ADD COMMENT
Topic
Name
8+3 =