declare @T table(InvoiceId int, Details xml)
insert into @T
select 1001, '<Stuff Id="101" p="3" q="5"/><Stuff Id="102" q="4"/><Stuff Id="103"/>' union all
select 1002, '<Stuff Id="201"/><Stuff Id="202" q="2"/>'
select InvoiceId,
stuff((select ','+t.n.value('@Id', 'varchar(10)')
from Details.nodes('Stuff') as t(n)
for xml path('')), 1, 1, '') as IdDetails
from @T
Только уникальные значения для Id
.
select InvoiceId,
stuff((select ','+s.id
from Details.nodes('Stuff') as t(n)
cross apply (select t.n.value('@Id', 'varchar(10)')) as s(id)
group by s.id
for xml path('')), 1, 1, '') as IdDetails
from @T