I have the following xml in an xml column in sql server, it is in a table along with a id column -
<ArrayOfExtVar xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<ExtVar>
<Name xsi:type="xsd:string">Rate Code</Name>
<Value xsi:type="xsd:string">E46</Value>
</ExtVar>
<ExtVar>
<Name xsi:type="xsd:string">Middle Name</Name>
<Value xsi:type="xsd:string">Henry</Value>
</ExtVar>
</ArrayOfExtVar>
And I have the following XPath -
SELECT CustID,
ExtVars.value('(/ArrayOfExtVar/ExtVar/Value)[1]', 'Nvarchar(max)') AS RateCode,
ExtVars.value('(/ArrayOfExtVar/ExtVar/Value)[2]', ''Nvarchar(max)') AS MiddleInitial
FROM dbo.Customer
which is great but what I'd really like to do is query the xml by 'Name' rather than the index ([1]) as these may be stored in different orders from time to time.
Basically what I need to know is how can I query by the value of 'Name' something like -
'(/ArrayOfExtVar/ExtVar/Value)[Rate Code]'
Could I add an attribute to the ExtVars Node and query on that instead?