Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 960317
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T01:09:52+00:00 2026-05-16T01:09:52+00:00

Given the following XML (in an SQL column field called ‘xfield’): <data> <section> <item

  • 0

Given the following XML (in an SQL column field called ‘xfield’):

<data>
  <section>
    <item id="A">
      <number>987</number>
    </item>
    <item id="B">
      <number>654</number>
    </item>
    <item id="C">
      <number>321</number>
    </item>
  </section>
  <section>
    <item id="A">
      <number>123</number>
    </item>
    <item id="B">
      <number>456</number>
    </item>
    <item id="C">
      <number>789</number>
    </item>
  </section>
</data>

How do you obtain the following table structure (with A, B & C as the column names):

 A | B | C
987|654|321
123|456|789

Using SQL XQuery, I’m trying this (not surprisingly, it’s invalid):

SELECT
  data.value('(./section/item[@ID = "A"]/number/[1])', 'int') as A,
  data.value('(./section/item[@ID = "B"]/number/[1])', 'int') as B,
  data.value('(./section/item[@ID = "C"]/number/[1])', 'int') as C
FROM Table CROSS APPLY [xfield].nodes('/data') t(data)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T01:09:52+00:00Added an answer on May 16, 2026 at 1:09 am

    You’re nearly there.

    You need to use nodes() to shred the xml into the rows you want to work with – here, you want a resultset row for each section element, so shred with

    nodes('/data/section')
    

    Once you’ve done that, you just need to make your xpath [1] syntactically correct (and relative to the section nodes you will be ‘in’):

    data.value('(item[@id = "A"]/number)[1]', 'int') as A,
    data.value('(item[@id = "B"]/number)[1]', 'int') as B,
    data.value('(item[@id = "C"]/number)[1]', 'int') as C
    

    And voila:

    A           B           C
    ----------- ----------- -----------
    987         654         321
    123         456         789
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Specifically, given the following XML content in a column called metadata, how do I
Given the following XML structure: <root> <data>x</data> <details> <some_other_element>...</some_other_element> <collection> <element><a>1</a></element> <element><a>2</a></element> <element><a>3</a></element> <collection>
Given the following XML: <Contact> <ContactID>41111-f15a-4fa1-b643-47877608f557</ContactID> <ContactStatus>ACTIVE</ContactStatus> <Name>ABC Ltd</Name> <EmailAddress>xxx@xxx.co</EmailAddress> <SkypeUserName>xxxdemo</SkypeUserName> <Addresses> <Address> <AddressType>STREET</AddressType>
Given the following xml: <language>en-US</language> <provider>VenturesLLC</provider> <video> <original_spoken_locale>en-US</original_spoken_locale> <vendor_offer_code>TEST_VENDOR</vendor_offer_code> <release_date>2011-01-15</release_date> <title>Moving Forward</title> <vendor_id>ASDF_ING_2012</vendor_id> </video>
Given the following xml: <!-- file.xml --> <video> <original_spoken_locale>en-US</original_spoken_locale> <another_tag>somevalue</another_tag> </video> What would be
Given the following xml: <Title> <EST> <EST_Start_Date>2009-09-21</EST_Start_Date> <EST_End_Date>2015-12-31</EST_End_Date> <EST_Version> <Vendor_ID>asdf-200130</Vendor_ID> <Master_Type_HD_SD>SD</Master_Type_HD_SD> </EST_Version> <EST_Version> <Digital_SKU>205119</Digital_SKU>
Given the following XML, I would like to return all eventtitles where the eventtype
Given the following XML: <root> Pacman <format bold=1 italic=1>rules</format>! </root> What is a better
Given the following XML: <?xml version=1.0 encoding=UTF-8 ?> <?xml-stylesheet type=text/xsl href=form.xsl?> <Document> <Translations> <Translation
Given the following repository URL from my pom.xml how can I determine what the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.