Extract data from XML field in SQL Server 2005 table

Posted on 2011-05-11
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


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="" last-page="">
  <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="" 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>

How can I extract say the field value for the site_code etc using SQL?
Question by:itwex
    LVL 51

    Accepted Solution

    try this:
    declare @xml xml
    select top 1 @xml=xml_data from XmlImportTest;
    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


    Author Closing Comment

    works fine thank you

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Suggested Solutions

    The steps for moving the system databases to a new location are documented in the following technical article: However sometimes after the moving process is finished, though SQL i…
    Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
    Viewers will learn how the fundamental information of how to create a table.
    Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

    779 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

    11 Experts available now in Live!

    Get 1:1 Help Now