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

  • Home
  • SEARCH
  • 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 3842752
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T15:50:19+00:00 2026-05-19T15:50:19+00:00

Using MS SQL Server, I’ve got some data in an XML field (called XML

  • 0

Using MS SQL Server, I’ve got some data in an XML field (called XML), which is structured like this:

<Transaction01>
  <TransactionSetPurpose>Insert</TransactionSetPurpose>
  <POHeader>
    <PO_NBR>LG40016181</PO_NBR>
  </POHeader>
</Transaction01>

I’m trying to create a SQL query to fetch another column called SubmittedDate, along with the PO_NBR from this XML field. Being new to XPath, I’ve read numerous examples and tried both query and value, but I’ve not been successful yet. For example:

SELECT SubmittedDate, 
       XML.query('data(/POHeader/PO_NBR)') as PO_NBR
  FROM SubmitXML

This just gives me a empty column. After getting a working test from Quassnoi, I worked from his XML to mine, and discovered the problem is the xmlns and xmlns:i attributes in the root node:

<Transaction01 xmlns="http://services.iesltd.com/" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">

So how do I get around that?

  • 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-19T15:50:20+00:00Added an answer on May 19, 2026 at 3:50 pm
    SELECT  SubmittedDate, 
            XML.query('data(/Transaction01/POHeader/PO_NBR)') as PO_NBR
    FROM    SubmitXML
    

    You original XPath, /POHeader/PO_NBR, assumed that POHeader is the root node (which is not).

    A sample query to check:

    DECLARE @myxml XML
    SET @myxml = '
    <Transaction01>
      <TransactionSetPurpose>Insert</TransactionSetPurpose>
      <POHeader>
        <PO_NBR>LG40016181</PO_NBR>
      </POHeader>
    </Transaction01>'
    
    SELECT  @myxml.query('data(/Transaction01/POHeader/PO_NBR)')
    

    If Transaction01 is not always the root node (which is not a good thing), use this:

    SELECT  SubmittedDate, 
            XML.query('data(/*/POHeader/PO_NBR)') as PO_NBR
    FROM    SubmitXML
    

    Generally, XML schema assumes that the tag names are fixed and the variable parts go to the data of the nodes and the attributes rather than into their names, like this:

    <Transaction id='01'>
      <TransactionSetPurpose>Insert</TransactionSetPurpose>
      <POHeader>
        <PO_NBR>LG40016181</PO_NBR>
      </POHeader>
    </Transaction>
    

    Update:

    You should declare the namespaces using WITH XMLNAMESPACES:

    DECLARE @myxml XML
    SET @myxml = '
    <Transaction01 xmlns="http://services.iesltd.com/" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
      <TransactionSetPurpose>Insert</TransactionSetPurpose>
      <POHeader>
        <PO_NBR>LG40016181</PO_NBR>
      </POHeader>
    </Transaction01>'
    
    ;
    WITH    XMLNAMESPACES
            (
            'http://services.iesltd.com/' AS m
            )
    SELECT  @myxml.query
            (
            'data(/*/m:POHeader/m:PO_NBR)'
            )
    

    Update 2:

    To sort:

    ;
    WITH    XMLNAMESPACES
            (
            'http://services.iesltd.com/' AS m
            )
    SELECT  SubmittedDate, 
            XML.value('(/*/m:POHeader/m:PO_NBR)[1]', 'NVARCHAR(200)') AS po_nbr
    FROM    SubmitXML
    ORDER BY
            po_nbr
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using sql server 2000, I would like to take my production data and put
Using SQL Server, which is the fastest or best practice method to use for
I'm using SQL Server 2000 to print out some values from a table using
I'm using SQL Server 2005, and I would like to know how to access
We are using SQL Server 2005, but this question can be for any RDBMS
Using SQL Server 2005 I have a field that contains a datetime value. What
I have made a sql database using sql server. This database include images. When
Using SQL Server, how do I split a string so I can access item
Using SQL Server 2005 and Management Studio how do I insert a picture into
When using SQL Server Management Studio from SQL Server 2005, I can connect to

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.