Solved

Parse XML stored in Varchar(Max) field

Posted on 2012-03-23
1
762 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 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

Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
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…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

717 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