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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:15:59+00:00 2026-05-25T15:15:59+00:00

I have an xml fragment like the following example and I’d like to get

  • 0

I have an xml fragment like the following example and I’d like to get the list of columns out and turn them into a comma-separated list but I’m really struggling to figure out the syntax for working with the XML data type and the documentation I’ve found reads like Korean stereo instructions 🙁

<ROOT>
<DATAVIEW>sp_Demo</DATAVIEW>
<WHERECLAUSE>X=4</WHERECLAUSE>
<COLUMNS>
  <COLUMN>ImageHTML</COLUMN>
  <COLUMN>Task_ID</COLUMN>
</COLUMNS>
</ROOT>

Can anyone point me at a beginners example or give me a pointer on how to do this?

From the XML fragment above I’d like to return a string containing “ImageHTML,Task_ID”

Well it’s not pretty but this does the trick, thanks for the pointer Louis.

DECLARE MY_CURSOR Cursor
FOR 
Select  Substring(Substring(CAST(T.Item.query('.') AS varchar(50)),0,LEN(CAST(T.Item.query('.') AS varchar(50)))-8),9,LEN(CAST(T.Item.query('.') AS varchar(50))))
From @Data.nodes('ROOT/COLUMNS/COLUMN') AS T(Item) 

    Open MY_CURSOR
    DECLARE @ColumnName varchar(50)
    DECLARE @RunningTotal varchar(2000)
    SET @RunningTotal=''

    FETCH NEXT FROM MY_CURSOR INTO @ColumnName
    WHILE (@@Fetch_STATUS<>-1)
    BEGIN
    IF (@@FETCH_STATUS<>-2)
    IF LEN(@RunningTotal)>0 SET @RunningTotal=@RunningTotal+','
    SET @RunningTotal=@RunningTotal+@ColumnName

    FETCH NEXT FROM MY_CURSOR INTO @ColumnName
    END
    CLOSE MY_CURSOR
    DEALLOCATE MY_CURSOR

    PRINT @RunningTotal

Final Update: While my cursor version worked, Akash presented a much neater solution below.

  • 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-25T15:16:00+00:00Added an answer on May 25, 2026 at 3:16 pm

    You do not need a cursor here. You need two things:

    • the xml data type methods .nodes() and .value()
    • a well-known trick for producing a comma-separated list from a rowset

    Setup:

    DECLARE @xml xml
    
    SET @xml = '
    <ROOT>
    <DATAVIEW>sp_Demo</DATAVIEW>
    <WHERECLAUSE>X=4</WHERECLAUSE>
    <COLUMNS>
      <COLUMN>ImageHTML</COLUMN>
      <COLUMN>Task_ID</COLUMN>
    </COLUMNS>
    </ROOT>
    '
    

    Output:

    DECLARE @output nvarchar(max)
    
    SELECT
        @output = COALESCE(@output + ', ', '') + C.value('text()[1]', 'nvarchar(MAX)')
    FROM
        @xml.nodes('//COLUMN') X(C)
    
    SELECT @output
    

    Explanation:

    .nodes() performs a shred of xml into rows. In this case, it interrogates the xml with the xpath //COLUMN and produces a rowset with each matching node as a row. This rowset is aliased to a table named X with one column C.

    In the SELECT clause, each xml node has its text() value extracted with .value()

    The @output trick is a standard method for producing a comma-delimited list.

    Output:

    ImageHTML, Task_ID
    

    Hopefully these terms and methods will enable you to find the apropriate docs and examples.

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

Sidebar

Related Questions

I have a similar question like XML indenting when injecting an XML string into
I have the following simple XML fragment shown below. What would the T-SQL look
The problem is this: I have an XML fragment like so: <fragment>text1 <a>inner1 </a>text2
I have a xml fragment like below <Detail uid=6> <![CDATA[ <div class=heading>welcome to my
I have an xml layout for a Fragment (android.support.v4.app.Fragment). When this Fragment is added
I have XML in the following form that I want to parse with PHP
I have xml like this: <rule> <word>I</word> <word>need</word> <word>more</word> <marker> <word>money</word> </marker> <word>now</word> </rule>
I have XML that looks like this: <Parameter Name=parameter name Value=parameter value /> which
I have xml like this: <configurationData> <path name='b'> <path name='a'> <setting name='s1'> ![CDATA[XXXX]] </setting>
I have a schema fragment that looks like <xs:element name=dataValue> <xs:complexType> <xs:sequence> <xs:element name=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.