Here i've a xml in a table 'Test' as below
Id Input
------------------------------------------------------
1 <DeviceList xmlns="www.domain.com/devicelist">
<Device xmlns="" Name="Device1">1</Device>
<Device xmlns="" Name="Device2">2</Device>
</DeviceList>
2 <DeviceList xmlns="www.domain.com/devicelist">
<Device xmlns="" Name="Device3">3</Device>
<Device xmlns="" Name="Device4">4</Device>
</DeviceList>
3 <DeviceList>
<Device>4</Device>
<Device>5</Device>
</DeviceList>
my expected result would be as below,
Id DeviceIds
--------------
1 1,2
2 3,4
3 4,5
My query,
SELECT Id,
STUFF((SELECT
', ' + CAST(Id.query('./text()') as VARCHAR(MAX)) FROM Input.nodes('/DeviceList/Device') AS Projectors(Id) FOR XML PATH('')), 1, 1, '') AS DeviceIds
FROM test;
But, this query return below result,
Id DeviceIds
-------------
1 NULL
2 NULL
3 4,5
Anybody help/suggest me on this.