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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T01:45:01+00:00 2026-05-20T01:45:01+00:00

Say I have the following table (where History is an xml column): Id Value

  • 0

Say I have the following table (where History is an xml column):

Id      Value     History
1       "Hello"   <History>
                    <Node date="1-1-2011">World</Node>
                    <Node date="1-2-2011">Foo</Node>
                    <Node date="1-3-2011">Bar</Node>
                  </History>
2       "Baz"     <History>
                    <Node date="1-1-2011">Buzz</Node>
                    <Node date="1-2-2011">Fizz</Node>
                    <Node date="1-3-2011">Beam</Node>
                  </History>

And from that I wanted to select a new table like:

HistoryId   Id      Value       Date
1           1       "World"     1-1-2011
2           1       "Foo"       1-2-2011
3           1       "Bar"       1-3-2011
4           2       "Buzz"      1-1-2011
5           2       "Fizz"      1-2-2011
6           2       "Beam"      1-3-2011

How would I do that?

If it were just a standalone xml value I could do something like this:

DECLARE @xml2 XML = '
<History>
  <Node date="1-1-2011">World</Node>
  <Node date="1-2-2011">Foo</Node>
  <Node date="1-3-2011">Bar</Node>
</History>'

SELECT 
    x.value('(@date)[1]','date') AS [Date]
    ,x.value('.', 'nvarchar(50)') AS Value
FROM @xml2.nodes('/History/Node') temp(x) 

But I’m not sure how to do it when the XML data is part of a table column. I could probably figure out a way to do it imperatively with a cursor but I was wondering if there’s a more elegant declarative solution that I’m not aware of.

  • 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-20T01:45:02+00:00Added an answer on May 20, 2026 at 1:45 am

    Use a cross apply

    declare @T table (Id int, Value nvarchar(50), History xml)
    insert into @T values (1, 'Hello','
    <History>
      <Node date="1-1-2011">World</Node>
      <Node date="1-2-2011">Foo</Node>
      <Node date="1-3-2011">Bar</Node>
    </History>')
    insert into @T values (2, 'Baz','
    <History>
      <Node date="1-1-2011">Buzz</Node>
      <Node date="1-2-2011">Fizz</Node>
      <Node date="1-3-2011">Beam</Node>
    </History>')
    
    select
        Id,
        h.n.value('.', 'varchar(10)') as Value,
        h.n.value('@date', 'varchar(10)') as Date
    from @T
        cross apply history.nodes('History/Node') h(n)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have the following table: Value Time 0 15/06/2012 8:03:43 PM 1
Let's say you have the following table (the column of interest here is cid
Say I have the following table: ID Value -- ---- 1 1 1 1
Say I have the following table item_a_id, item_b_id, value Where item_a_id and item_b_id are
Let's say I have the following table: id | letter | date -------------------------------- 1
Let's say I have the following table first last value tony jones 1 james
Let's say I have following table like this <!DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0
Let's say we have the following table: CREATE TABLE T ( ID INT, String1
The idea is that say you have the following table. ------------- | oID |
Let's say I have the following database table: record_id | record_date | record_value -----------+-------------+--------------

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.