I'm trying to parse / shred XML in sql server 2008 via TSQL, one of the nodes has a blank property in the following format with no value specified. I can't seem to retrieve that value and it throws off all the others (shifts the columns ?)
Here is the tsql code and a pic of what is wrong:
declare @doc nvarchar(4000)
set @doc = '
<Objects>
<Object>
<Property Name="Path">some path</Property>
<Property Name="InstanceName">some instance</Property>
<Property Name="result1">0.390630000000003</Property>
<Property Name="result2">63345649697265</Property>
</Object>
<Object>
<Property Name="Path">another path</Property>
<Property Name="InstanceName" />
<Property Name="result1">100</Property>
<Property Name="result2">1002</Property>
</Object>
</Objects>
'
SELECT
item.ref.value('(Property/text())[1]', 'nvarchar(128)') AS Path,
item.ref.value('(Property/text())[2]', 'nvarchar(128)') AS InstanceName,
item.ref.value('(Property/text())[3]', 'nvarchar(128)') AS result1,
item.ref.value('(Property/text())[4]', 'nvarchar(128)') AS result2
FROM (SELECT CAST(@doc AS XML) AS feedXml) feeds(feedXml)
CROSS APPLY feedXml.nodes('/Objects/Object') AS item(ref)
When you run the query notice that the column InstanceName is populated with the value from the other column instead of blank, empty or null.
Any help is appreciated.