?
Solved

Extract data from XML field in SQL Server 2005 table

Posted on 2011-05-11
2
Medium Priority
?
288 Views
Last Modified: 2012-06-21
I have a sql server table

CREATE TABLE [dbo].[XmlImportTest](

                [xmlFileName] [varchar](300) COLLATE Latin1_General_CI_AS NULL,

                [xml_data] [xml] NULL

) ON [PRIMARY]

which contains xml

<form name="esb_recording" application-name="esb_recording" application-id="102" document-id="441" producer="Penvision Formidable Application Server" export-time="2011-04-29T15:03:17+01:00" first-page="83.17.3.121" last-page="83.17.3.121">
  <submit penid="0x000001e3000214ed" penid-dashed="ATD-AAF-BUX-QE" first-stroke-time="2011-04-29T14:42:07+01:00" last-stroke-time="2011-04-29T14:43:42+01:00" total-time="0:01:35" email="cryan@futurerange.ie" phone="+353868795846" />
  <field type="boxed" name="site_code" normalized-score="0.512" resemblance-score="0.579">D0173TPI</field>
  <field type="boxed" name="mon_day_1" normalized-score="0.726" resemblance-score="0.747">4567895</field>
  <field type="boxed" name="tues_day_1" normalized-score="0.697" resemblance-score="0.816">6432567</field>
  <field type="boxed" name="wed_day_1" normalized-score="0.624" resemblance-score="0.64">8943245</field>
  <field type="boxed" name="thur_day_1" normalized-score="0.634" resemblance-score="0.554">6789432</field>
  <field type="boxed" name="fri_day_1" normalized-score="0.718" resemblance-score="0.898">5678943</field>
  <field type="boxed" name="sat_day_1" normalized-score="0.753" resemblance-score="0.843">2456789</field>
</form>

How can I extract say the field value for the site_code etc using SQL?
0
Comment
Question by:itwex
2 Comments
 
LVL 61

Accepted Solution

by:
HainKurt earned 2000 total points
ID: 35738311
try this:
declare @xml xml
select top 1 @xml=xml_data from XmlImportTest;

select 
Field.value('.', 'nvarchar(max)') v,
Field.value('(@normalized-score)[1]','nvarchar(max)') ns
from @xml.nodes('//form/field') Fields(Field)
where Field.value('(@name)[1]','nvarchar(max)')='site_code'

Open in new window

0
 

Author Closing Comment

by:itwex
ID: 35738670
works fine thank you
0

Featured Post

Industry Leaders: 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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
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.
Suggested Courses

839 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