I have a database where I am trying to query a column where there is XML stored as ntext in our SQL Server. Why is it not stored as XML? Not sure but I can't change it so I have worked to try and convert it to some XML format. At this point I have been able to run this to get rows of entries that are treated as XML.
select top 5 cast(cast(ATTR_XML as varchar(max)) as XML)
from dbo.table1
This resulted in the following that is recognized as XML
<ArrayOfNameValue xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<NameValue>
<Name>Form_Submit Request_3E6791F5B9884EE2B335BC1F1E1285C5</Name>
<Value xsi:type="xsd:string">ProjectX-LIB-46c3219d-1fe0-4abb-b559-efb8a3c35505,AgileForm.34</Value>
</NameValue>
<NameValue>
<Name>Form</Name>
<Value xsi:type="xsd:string">ProjectX-LIB-46c3219d-1fe0-4abb-b559-efb8a3c35505,AgileForm.69</Value>
</NameValue>
<NameValue>
<Name>Form_Update Request_E4FA8434326E42ADB6222978214E93E9</Name>
<Value xsi:type="xsd:string">ProjectX-LIB-46c3219d-1fe0-4abb-b559-efb8a3c35505,AgileForm.69</Value>
</NameValue>
<NameValue>
<Name>//</Name>
<Value xsi:type="xsd:string"><?xml version="1.0" encoding="utf-8"?><pd:Data xmlns:pd="http://www.test.com/XMLSchema"><pd:Request_Source system_name="Zo5ZCqLRRfV" datatype="object" listID="" listValue="">Coastal Urban</pd:Request_Source><pd:Request_Category system_name="ojZD66m94eu" datatype="object" listID="" listValue="">Information Request</pd:Request_Category><pd:Date_of_Request system_name="KNc4mzCYmYL" datatype="date" listID="" listValue="">05 Feb 2015<pd:Request_Date system_name="QXf1HQ7EFUZ" datatype="string" listID="" listValue="">06-Feb-15 03:38:58</pd:Request_Date><pd:Request_Priority system_name="" datatype="" listID="" listValue=""></pd:Request_Priority></pd:Data></Value>
</NameValue>
</ArrayOfNameValue>
I am trying to extract the element name and value from this
<Value xsi:type="xsd:string"><?xml version="1.0" encoding="utf-8"?><pd:Data xmlns:pd="http://www.test.com/XMLSchema"><pd:Request_Source system_name="Zo5ZCqLRRfV" datatype="object" listID="" listValue="">Coastal Urban</pd:Request_Source><pd:Request_Category system_name="ojZD66m94eu" datatype="object" listID="" listValue="">Information Request</pd:Request_Category><pd:Date_of_Request system_name="KNc4mzCYmYL" datatype="date" listID="" listValue="">05 Feb 2015<pd:Request_Date system_name="QXf1HQ7EFUZ" datatype="string" listID="" listValue="">06-Feb-15 03:38:58</pd:Request_Date><pd:Request_Priority system_name="" datatype="" listID="" listValue=""></pd:Request_Priority></pd:Data></Value>
I'd like to extract Request_Category is 'Information Request'.
How can I pull these out using XML methods and insert them into two columns in my query so I have Name and Value?
Thanks
DECLARE @XML XML =
'<ArrayOfNameValue xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<NameValue>
<Name>Form_Submit Request_3E6791F5B9884EE2B335BC1F1E1285C5</Name>
<Value xsi:type="xsd:string">ProjectX-LIB-46c3219d-1fe0-4abb-b559-efb8a3c35505,AgileForm.34</Value>
</NameValue>
<NameValue>
<Name>Form</Name>
<Value xsi:type="xsd:string">ProjectX-LIB-46c3219d-1fe0-4abb-b559-efb8a3c35505,AgileForm.69</Value>
</NameValue>
<NameValue>
<Name>Form_Update Request_E4FA8434326E42ADB6222978214E93E9</Name>
<Value xsi:type="xsd:string">ProjectX-LIB-46c3219d-1fe0-4abb-b559-efb8a3c35505,AgileForm.69</Value>
</NameValue>
<NameValue>
<Name>//</Name>
<Value xsi:type="xsd:string"><?xml version="1.0" encoding="utf-8"?><pd:Data xmlns:pd="http://www.test.com/XMLSchema"><pd:Request_Source system_name="Zo5ZCqLRRfV" datatype="object" listID="" listValue="">Coastal Urban</pd:Request_Source><pd:Request_Category system_name="ojZD66m94eu" datatype="object" listID="" listValue="">Information Request</pd:Request_Category><pd:Date_of_Request system_name="KNc4mzCYmYL" datatype="date" listID="" listValue="">05 Feb 2015<pd:Request_Date system_name="QXf1HQ7EFUZ" datatype="string" listID="" listValue="">06-Feb-15 03:38:58</pd:Request_Date><pd:Request_Priority system_name="" datatype="" listID="" listValue=""></pd:Request_Priority></pd:Data></Value>
</NameValue>
</ArrayOfNameValue>';
SELECT
bar.value('local-name(.)','VARCHAR(10)') as ColName,
bar.value('./.','VARCHAR(MAX)') as Value
FROM
@xml.nodes('/ArrayOfNameValue/NameValue/*') AS foo(bar)
Results:
ColName Value
---------- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Name Form_Submit Request_3E6791F5B9884EE2B335BC1F1E1285C5
Value ProjectX-LIB-46c3219d-1fe0-4abb-b559-efb8a3c35505,AgileForm.34
Name Form
Value ProjectX-LIB-46c3219d-1fe0-4abb-b559-efb8a3c35505,AgileForm.69
Name Form_Update Request_E4FA8434326E42ADB6222978214E93E9
Value ProjectX-LIB-46c3219d-1fe0-4abb-b559-efb8a3c35505,AgileForm.69
Name //
Value <?xml version="1.0" encoding="utf-8"?><pd:Data xmlns:pd="http://www.test.com/XMLSchema"><pd:Request_Source system_name="Zo5ZCqLRRfV" datatype="object" listID="" listValue="">Coastal Urban</pd:Request_Source><pd:Request_Category system_name="ojZD66m94eu" da
Is this what you were looking for? If anything, I hope its a good start.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.