Sql server rows values into column group by values with comma separated| sql Server Rows Values into Column|Convert Multiple Rows into One Comma Separated Values

I have the table like 

Example:


I want to group by SysName, but all the Code should be comma separated. 
like..

--------------------------
Code            SysName
--------------------------
5                  Ssst10
1,3,5            Ssst11
5,6,7,8,9,10  Ssst12
--------------------------

ANSWER:


Try this Query:
Select (Select STUFF (
(Select ',' + CONVERT(VARCHAR(50),Code)
from System 
where sysName = A.sysName
FOR XML PATH('')
)
,1,1,'') AS cols
) as Code,A.sysName from System as A group by A.sysName 

Result:

Post a Comment

0 Comments