I've been attempting to write this query but, so far, to no avail.
The following is some partial data from an XML column in SQL Server:
<DashboardWidgets>
<DashboardWidget id="14">
<EnumName>PersonalProgressIndividual</EnumName>
<OnOff>1</OnOff>
<Movable>0</Movable>
<Removable>0</Removable>
<SubItemData>
<SubItem id="1">
<OnOff>1</OnOff>
<Movable>1</Movable>
<Removable>1</Removable>
</SubItem>
<SubItem id="2">
<OnOff>1</OnOff>
<Movable>1</Movable>
<Removable>1</Removable>
</SubItem>
<SubItem id="3">
<OnOff>1</OnOff>
<Movable>1</Movable>
<Removable>1</Removable>
</SubItem>
<SubItem id="4">
<OnOff>0</OnOff>
<Movable>0</Movable>
<Removable>0</Removable>
</SubItem>
<SubItem id="6">
<OnOff>0</OnOff>
<Movable>0</Movable>
<Removable>0</Removable>
</SubItem>
</SubItemData>
</DashboardWidget>
</DashboardWidgets>
My goal is to query the table and retrieve a formatted string of IDs and values.
For an example, I would need to query the DashboardWidget node with the ID of 14 and build the string from the SubItemData child nodes contained within.
The string result required for a query of the dashboard widget with the ID of 14 would be:
"1,1,1,1|2,1,1,1|3,1,1,1|4,0,0,0|6,0,0,0"
I've been able to come close by extracting all the values but without any delimiters at all.
DECLARE
@companyID INT = 23
,@dwID INT = 14
;
DECLARE @xml xml
SELECT @xml = c.DashboardWidgetSettings FROM dbo.Company c WHERE c.CompanyID = @companyID;
SELECT
x.Rec.query('./SubItem').value('.', 'varchar(max)') AS 'SubItemData'
FROM @xml.nodes('/DashboardWidgets/DashboardWidget[@id=sql:variable("@dwID")]/SubItemData') as x(Rec)
;
Any help or a point in the right direction would be greatly appreciated.