Solved

Parse XML stored in Varchar(Max) field

Posted on 2012-03-23
1
758 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
1 Comment
 
LVL 37

Accepted Solution

by:
ValentinoV earned 500 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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

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

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

911 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now