Solved

Parse XML stored in Varchar(Max) field

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now