my table1 is :
col1 col2
C1 john
C2 alex
C3 piers
C4 sara
and so table 2:
If you wanted to do this task in oracle we can use listagg
and can accomplish this easily.
A possible equivalent available in SQL Server for listagg
is Stuff
So using stuff you can try with following query:
SELECT T2.Col1,
Stuff((SELECT ',' + CAST(T1.Col2 AS VARCHAR(100))
FROM T1
WHERE T2.Col2 LIKE T1.Col1
FOR Xml Path('')),
1,
1,
'')
FROM T2