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 5939265
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:48:13+00:00 2026-05-22T15:48:13+00:00

I have a database which has a table with an XML column. The XML

  • 0

I have a database which has a table with an XML column. The XML data has a bunch of child nodes which look something like this:

<test>
  <result id="1234">
    <data elementname="Message">some error message</data>
    <data elementname="Cat">Cat01</data>
    <data elementname="Type">WARNING</data>
  </result>
  <result id="5678">
    <data elementname="Message">some error message</data>
    <data elementname="Cat">Cat01</data>
    <data elementname="Type">WARNING</data>
  </result>
</test>

The Cat element can have a number of different values. I’m trying to create reports on this data, so one thing I’d like to do is get a list of all the categories througout our data. This is my query:

Select Id, XmlData.query('/test/result/data[@elementname = ''Cat''] ')  AS  Message
From Table
WHERE XmlData.exist('/test/result/data[@elementname = ''Cat'']') = 1
ORDER BY FriendlyName

This correctly gets all the rows in my table with this type of categorization (there’ll be other results in the same table without that element), but the categories are all combined into one column for each table record:

Id1, <data elementname="Cat">Cat01</data><data elementname="Cat">Cat01</data>
Id2, <data elementname="Cat">Cat01</data><data elementname="Cat">Cat01</data>

I’m including the Id column so it’s easy to see where the data is coming from, the main problem is that I can only get it to concatenate the values for each row – I need each of those data elements to have its own row, then maybe do a Select Distinct on the result.

Is there a way I can do that?

Thanks

  • 1 1 Answer
  • 2 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-22T15:48:13+00:00Added an answer on May 22, 2026 at 3:48 pm

    Always the Google after you post your question….

    Think I found the answer here: http://blogs.msdn.com/b/simonince/archive/2009/04/24/flattening-xml-data-in-sql-server.aspx

    SELECT  DISTINCT  cref.value('(text())[1]', 'varchar(50)') as Cat
    FROM   
          SGIS CROSS APPLY 
          Data.nodes('/test/result') AS Results(rref) CROSS APPLY
          rref.nodes('data[@elementname = ''Cat'']') AS Categories(cref)  
    

    Seems the key is the Cross Apply keywords

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table which has field with data type as DATETIME. I persist
I have an Excel file (which has data imported from Oracle 10G Database) one
I have a database table transaction which stores all the transaction made by users
I am having a table which has about 17 fields. I need to perform
I have an existing Access database which contains training records for employees. If an
I have created a procedure in dynamic SQL which has a select statement and
I am creating a database of structures that can be of different types. Each
I am working in asp.net MVC2 application and I have question related to design
I am trying to understand what information are needed to be sent in a

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.