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

The Archive Base Latest Questions

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

In SQL 2005, is there a way to convert the following xml into a

  • 0

In SQL 2005, is there a way to convert the following xml into a table?

<root>
  <r>
    <data>"col1"</data>
    <data>"col2"</data>
    <data>"col3"</data>
  </r>
  <r>
    <data>"data1"</data>
    <data>""</data>
    <data>"data3"</data>
  </r>
  <r>
    <data>"data"</data>
    <data>"data"</data>
    <data>"data"</data>
  </r>
</root>

I want the output to be

col1 col2 col3
----------------
data      data3
data data data

The xml can have different number of columns so the solution needs to account for this.

Thanks in advance.

  • 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:07:34+00:00Added an answer on May 22, 2026 at 3:07 pm
    declare @xml xml
    set @xml = 
    '<root>
      <r>
        <data>"col1"</data>
        <data>"col2"</data>
        <data>"col3"</data>
      </r>
      <r>
        <data>"data1"</data>
        <data>""</data>
        <data>"data3"</data>
      </r>
      <r>
        <data>"data"</data>
        <data>"data"</data>
        <data>"data"</data>
      </r>
    </root>'
    
    declare @SQL nvarchar(max)
    set @SQL = ''
    
    select @SQL = @SQL + ',replace(r.r.value(''data['+
             cast(T.rn as nvarchar(10))+
             ']'', ''varchar(10)''), ''"'','''') as '+
             quotename(replace(T.ColName, '"', '')) 
    from
    (
      select
        r.r.value('.', 'sysname') as ColName,
        row_number() over(order by (select 1)) as rn
      from @xml.nodes('/root/r[1]/data') r(r)
    ) as T
    
    set @SQL = 'select '+stuff(@SQL, 1, 1, '')+
            ' from @x.nodes(''/root/r[position()>1]'') r(r)'
    
    exec sp_executesql @SQL, N'@x xml', @x = @xml
    

    Since I use dynamic SQL here it is appropriate to suggest reading The Curse and Blessings of Dynamic SQL.

    An explanation of what is going on.

    This query is used to get the columns names from the first r node:

    select
        r.r.value('.', 'varchar(10)') as ColName,
        row_number() over(order by (select 1)) as rn
      from @xml.nodes('/root/r[1]/data') r(r)
    

    /root/r[1] makes sure we get the first row. row_number() enumerates the columns making a connection between a number and the column name.

    The resulting query in @SQL is this:

    select 
      replace(r.r.value('data[1]', 'varchar(10)'), '"','') as [col1],
      replace(r.r.value('data[2]', 'varchar(10)'), '"','') as [col2],
      replace(r.r.value('data[3]', 'varchar(10)'), '"','') as [col3] 
    from @xml.nodes('/root/r[position()>1]') r(r)
    

    /root/r[position()>1] gets all r nodes except the first one. The 1 in data[1] comes from row_number() and [col1] comes from the corresponding column name. quotename() adds the brackets [] to the column alias. Without quotename() this query could be used for SQL injection. replace() is used to remove " from the string. It will remove all occurrences of " so if you expect " to be part of a value you could use substring() to remove " instead.

    I have used varchar(10) as the size of column data. You should modify that to whatever you need.

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

Sidebar

Related Questions

Is there a way to lock a row in the SQL 2005-2008 database without
Is there a way to restrict a specific SQL 2005 login on a Microsoft
Is there a way to get stored procedures from a SQL Server 2005 Express
Is there a way to easily sort in SQL Server 2005 while ignoring hyphens
Is there any way to use inheritance in database (Specifically in SQL Server 2005)?
In SQL Server 2005, is there a way of deleting rows and being told
In SQL Server 2005, is there a way for a trigger to find out
In SQL Server 2005, is there a way I can create a INSERT script
In sql server 2005 , inside an update trigger is there a way to
Is there an automatic way in SQL Server 2005 to create a database from

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.