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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T18:24:46+00:00 2026-05-15T18:24:46+00:00

I have this XML in T-SQL: <Elements> <Element> <Index>1</Index> <Type>A</Type> <Code>AB</Code> <Time>1900-01-01T10:21:00</Time> </Element> <Element>

  • 0

I have this XML in T-SQL:

<Elements>
    <Element>
        <Index>1</Index>
        <Type>A</Type>
        <Code>AB</Code>
        <Time>1900-01-01T10:21:00</Time>
    </Element>
    <Element>
        <Index>2</Index>
        <Type>M</Type>
        <Code>AL</Code>
        <Time>1900-01-01T10:22:00</Time>
    </Element>
</Elements>

And I want to retrieve it as a table:

Index    FieldName    FieldValue
-------- ------------ ----------
1        Index        1
1        Type         A
1        Code         AB
1        Time         1900-01-01T10:21:00
2        Index        2
2        Type         M
2        Code         AL
2        Time         1900-01-01T10:22:00

Of course, what I’m looking for here is to pivot the Element nodes into rows, but I can’t get more than just the field value OR the index at a time…

select
--  r.value('.[1]', 'nvarchar(10)') Value,
--  r.value('fn:local-name(.)', 'nvarchar(50)') FieldName
    r.value('Index[1]', 'nvarchar(10)') f,
    r.value('./node()[fn:local-name(.)]', 'nvarchar(10)') v
from
    @content.nodes('/Elements/*') as records(r)
  • 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-15T18:24:46+00:00Added an answer on May 15, 2026 at 6:24 pm

    You could try something like this:

    SELECT
        El.Elem.value('(Index)[1]', 'int'),
        SubEl.SubElem.value('local-name(.)', 'varchar(100)') AS 'Field Name',
        SubEl.SubElem.value('.', 'varchar(100)') AS 'Field Value'
    FROM
        @content.nodes('/Elements/Element') AS El(elem)
    CROSS APPLY
        El.Elem.nodes('*') AS SubEl(SubElem)
    

    That seems to produce your desired output in my test case.

    You basically need to select all /Elements/Element nodes in a first step, get their index value, and then in a second step, select all child nodes (/*)for any given <Element> node.

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

Sidebar

Related Questions

I have this xml from sql, and I want to do the same by
I have this sql script DECLARE @XmlStr XML SET @XmlStr = '<EmployeeID> <Employee>48f9194f-8d46-e111-8849-0050569445f1</Employee> <Employee>7d725561-8d46-e111-8849-0050569445f2</Employee>
I have this xml file <?xml version=1.0 encoding=UTF-8?> <bo:C837ClaimParent xsi:type=bo:C837ClaimParent xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance xmlns:bo=http://somelongpathHere/process/bo> <claimAux> ...
I have a XML structure in a XML column on a SQL Server table
Assuming I have a SQL Server 2005 table with an xml column containing the
I have a table in SQL Server 2000 with a text field containing XML
I have a table called XML (in SQL Server 2008) and it has a
I have this XML in a column in my table: <keywords> <keyword name=First Name
I have a SQL Server 2005 table like this: create table Taxonomy( CategoryId integer
I have an XML document snippet that matches this XSD: <xs:complexType name=QuestionType> <xs:sequence> <xs:element

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.