This is basicaly the concatenation of strings (like a sum for numeric).
An alternative could be:
declare @tblNames table([Name] varchar(100))
insert @tblNames values ('Lucian')
insert @tblNames values ('Terry')
insert @tblNames values ('Jane')
select SUBSTRING((SELECT ',' + rtrim([Name])FROM @tblNames
FOR XML PATH('')) , 2, 7998) AS Name_List
Result would be Lucian,Terry,Jane
This is usefull when you need to aggregate a column and together with this concatenate a string column. ex.
if exists(select 1 from information_schema.tables where table_name = 'tblTest')
drop table tblTest
Create table tblTest(id int , Category varchar(3),
AnyNumber int, SCode varchar(255))
insert tblTest(Id, Category, AnyNumber, SCode)
select 1, 'Air', 15, 'SCode0' Union All
select 1, 'Air', 15, 'SCode1' Union All
select 1, 'Acc', 10, 'AccSCode1' Union All
select 1, 'Acc', 10, 'AccSCode2' Union All
select 2, 'Air', 9, 'AirSCode4' Union All
select 2, 'Air', 15, 'AirSCode5' Union All
select 3, 'Air', 20, 'AirSCode6' Union All
select 3, 'Air', 13, 'AirSCode7' Union All
select 3, 'Trf', 14, 'TrfSCode1' Union All
select 3, 'Trf', 16, 'TrfSCode2'
--select * from tblTest
select T.Id, Category,
sum(T.AnyNumber) as SumNbr
,SUBSTRING((SELECT ',' + rtrim(SCode)FROM tblTest T2
WHERE T.Id = T2.Id and T.Category = T2.Category
FOR XML PATH('')) , 2, 7998) AS List_SCodes
from tblTest T
group by Id, Category
drop table tblTest
Give result:
(10 row(s) affected)
Id Category SumNbr List_SCodes
----------- -------- ----------- ----------------------------------------------------------
1 Acc 20 AccSCode1,AccSCode2
1 Air 30 SCode0,SCode1
2 Air 24 AirSCode4,AirSCode5
3 Air 33 AirSCode6,AirSCode7
3 Trf 30 TrfSCode1,TrfSCode2
(5 row(s) affected)