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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:28:13+00:00 2026-06-16T00:28:13+00:00

I am using SQL Server 2008 and I want to retrieve XML elements and

  • 0

I am using SQL Server 2008 and I want to retrieve XML elements and return the element itself and its attributes without any sub elements and without the text(). Eg the following XML has 4 nodes (doc, apple, b, banana):

<doc>
<apple type="bramley"> tasty <b>yum!</b> </apple>
<banana color="yellow" shape="bendy"> nice in smoothies </banana>
</doc>

I would like to return:

<doc/>
<apple type="bramley"/>
<b/>
<banana color="yellow" shape="bendy"/>

Eg doc should be returned without any sub-nodes and apple should be returned without the b sub-node. But the problem is, if I use the SQL Server nodes and query method I can’t remove the sub-nodes. Using SQL Server 2008 the closest I have is:

declare @x xml = '<doc>
  <apple type="bramley"> tasty <b>yum!</b> </apple>
  <banana color="yellow" shape="bendy"> nice in smoothies </banana>
</doc>';

select 
  c.query('local-name(.)') as Node,
  c.query('for $e in . return <xx> {$e/@*} </xx>') as Attr
from @x.nodes('//*') as T(c);

This gets the name of each node (using local-name) and the attributes of the node and returns:

Node    Attr
----    ----
doc     <xx />
apple   <xx type="bramley" />
b       <xx />
banana  <xx color="yellow" shape="bendy" />

I realize I can process this result, convert Attr to varchar, replace xx by the Node column and convert back to XML. But is there an easier way without string manipulation?

PS: If it helps, I don’t mind if the solution uses SQL Server 2008 or SQL Server 2012.

  • 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-06-16T00:28:14+00:00Added an answer on June 16, 2026 at 12:28 am

    Usually you would use element construction with dynamic tag names, but SQL Server doesn’t support this:

    declare @x xml = '<doc>
      <apple type="bramley"> tasty <b>yum!</b> </apple>
      <banana color="yellow" shape="bendy"> nice in smoothies </banana>
    </doc>';
    
    select 
      c.query('local-name(.)') as Node,
      c.query('for $e in . return element { local-name($e) } { $e/@* } </xx>') as Attr
    from @x.nodes('//*') as T(c);
    

    As an XQuery Update alternative (tested with SQL Server 2012), we can fetch all nodes (with all contents) and delete their subnotes.

    DECLARE @x xml = '<doc>test
      <apple type="bramley"> tasty <b>yum!</b> </apple>
      <banana color="yellow" shape="bendy"> nice in smoothies </banana>
    </doc>';
    
    -- Fetch all nodes
    SET @x = @x.query('//*')
    -- Delete all subnodes
    SET @x.modify('delete /*/node()')
    
    select 
      c.query('.') as NewNode
    from @x.nodes('/*') as T(c);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using SQL Server 2008 Enterprise. I want to see any active SQL
I am using SQL Server 2008 R2 and I want to add a non-clustered
I am using SQL Server 2008 Enterprise version. I want to know if I
I want to develop a generic CRM application using SQL Server 2008. The application
I want to write a code to backup my Sql Server 2008 Database using
I am using SQL Server 2008 R2 I just want to test if something
I am using SQL Server 2008 Enterprise and I want to use SQL Server
I am using SQL Server 2008 express and i want to import .mdf and
I am using SQL Server 2008. I want to write a query that gives
I am using SQL Server 2008 Enterprise on Windows Server 2008. I want to

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.