?
Solved

Parse XML stored in Varchar(Max) field

Posted on 2012-03-23
1
Medium Priority
?
763 Views
Last Modified: 2012-05-10
I need to return specific values from XML stored in a varchar(max) field in ms sql server 2008.

MyTable
TransID (PK,int,not null)
Body (varchar(max),null)

select AgreementNumber, FieldName, OldValue, NewValue from (select Body from MyTable where TransID = 1)  

Return

AgreementNumber       FieldName      OldValue          NewValue
123456789               Due Date        8/20/2011        8/26/2011
123456789               Credits Paid      99.71            99.71
.......ad infinitum

Sample XML from Body column

<?xml version="1.0"?>
<EditAgreement>
<AgreeID>1050</AgreeID>
<AgreementNumber>60101050</AgreementNumber>
<CustomerName>Blah blah</CustomerName>
<Edits>
<FieldName>Due Date</FieldName>
<OldValue>8/20/2011</OldValue>
<NewValue>8/26/2011</NewValue>
</Edits>
<Edits>
<FieldName>Credits Paid</FieldName>
<OldValue>99.71</OldValue>
<NewValue>99.71</NewValue>
</Edits>
</EditAgreement>
0
Comment
Question by:griffaw
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 37

Accepted Solution

by:
ValentinoV earned 2000 total points
ID: 37764599
See code sample below.  I've used a variable to imitate your Body varchar(max) column.

First step is to convert it to the xml data type.  Then some xml functions are used to retrieve the data.

Note: you may have to adapt the data types, I've used varchar(100) everywhere.

Additional info:
XML value() method
XML nodes() method
The APPLY operator

declare @xml varchar(max) = 'Your XML Sample';

with XMLData as (
      select convert(xml, @xml) BodyXML
)
select BodyXML.value('(./EditAgreement/AgreeID)[1]', 'varchar(100)') AgreeID
      , edit.value('(./FieldName)[1]', 'varchar(100)') FieldName
      , edit.value('(./OldValue)[1]', 'varchar(100)') OldValue
      , edit.value('(./NewValue)[1]', 'varchar(100)') NewValue
from XMLData
cross apply BodyXML.nodes('/EditAgreement/Edits') t(edit)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question