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 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
  • 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-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 in my database which has information about a bunch of
I have a column in my database table which has lows of text and
I have a database structure that has a Person table which contains fields such
I have a database which has very similar rows within the same table. Those
I have a table in my database (Mysql) which has 6 columns. I am
I have a mysql database which has grown to over 200 tables in it.
I have to connect to a legacy postgres database which has ENCODING = 'SQL_ASCII';
Problem: I have an address field from an Access database which has been converted
I have a Rails project which has a Postgres database for the actual application
I have a stored procedure which takes an XML parameter and inserts the data

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.