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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T17:20:10+00:00 2026-05-15T17:20:10+00:00

I’m using a generic system for reporting which takes data from a database view

  • 0

I’m using a generic system for reporting which takes data from a database view (SQL Server 2005). In this view I had to merge data from one-to-many relations in one row and used the solution described by priyanka.sarkar in this thread: Combine multiple results in a subquery into a single comma-separated value. The solution uses SQLXML for merging the data (subquery):

SELECT STUFF(
    (    SELECT ', ' + Name 
         FROM MyTable _in 
         WHERE _in.ID = _out.ID 
         FOR XML PATH('')),        -- Output multiple rows as one xml type value,
                                   -- without xml tags
    1, 2, '')      -- STUFF: Replace the comma at the beginning with empty string
FROM MyTable _out 
GROUP BY ID        -- Removes duplicates

That works perfectly (it’s not even that heavy in performance) except my data now gets XML encoded (& => & etc.) by SQLXML -I didn’t want XML data after all, I just used this as a trick- and because of the generic system I can’t code around this to clean it up so the encoded data goes straight to the report. I can’t use stored procedures with the generic system so CURSOR-merging or COALESCE-ing is not an option here…

So what I’m looking for is a manner in T-SQL that lets me decode the XML again, or even better: avoids SQLXML from encoding it. Obviously I could write a stored function that does this, but I’d prefer a built-in, more safe manner…

Thanks for your help…

  • 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-15T17:20:11+00:00Added an answer on May 15, 2026 at 5:20 pm

    If you specify type as an option to for xml, you can use an XPath query to convert the XML type back to a varchar. With an example table variable:

    declare @MyTable table (id int, name varchar(50))
    
    insert @MyTable (id, name) select 1, 'Joel & Jeff'
    union all select 1, '<<BIN LADEN>>'
    union all select 2, '&&BUSH&&'
    

    One possible solution is:

    select  b.txt.query('root').value('.', 'varchar(max)')
    from    (
            select  distinct id
            from    @MyTable
            ) a
    cross apply
            (
                select  CASE ROW_NUMBER() OVER(ORDER BY id) WHEN 1 THEN '' 
                            ELSE ', ' END + name
            from    @MyTable
            where   id = a.id
            order by 
                    id
            for xml path(''), root('root'), type
            ) b(txt)
    

    This will print:

    Joel & Jeff, <<BIN LADEN>>
    &&BUSH&&
    

    Here’s an alternative without XML conversions. It does have a recursive query, so performance mileage may vary. It’s from Quassnoi’s blog:

    ;WITH   with_stats(id, name, rn, cnt) AS
            (
            SELECT  id, name,
                    ROW_NUMBER() OVER (PARTITION BY id ORDER BY name),
                    COUNT(*) OVER (PARTITION BY id)
            FROM    @MyTable
            ),
            with_concat (id, name, gc, rn, cnt) AS
            (
            SELECT  id, name,
                    CAST(name AS VARCHAR(MAX)), rn, cnt
            FROM    with_stats
            WHERE   rn = 1
            UNION ALL
            SELECT  with_stats.id, with_stats.name,
                    CAST(with_concat.gc + ', ' + with_stats.name AS VARCHAR(MAX)),
                    with_stats.rn, with_stats.cnt
            FROM    with_concat
            JOIN    with_stats
            ON      with_stats.id = with_concat.id
                    AND with_stats.rn = with_concat.rn + 1
            )
    SELECT  id, gc
    FROM    with_concat
    WHERE   rn = cnt
    OPTION  (MAXRECURSION 0)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 510k
  • Answers 510k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer After looking at the configuration schema at C:\Windows\System32\inetsrv\config\schema\IIS_schema.xml, I discovered… May 16, 2026 at 4:52 pm
  • Editorial Team
    Editorial Team added an answer For your specific example there is a standard solution: string.Join.… May 16, 2026 at 4:52 pm
  • Editorial Team
    Editorial Team added an answer A better code example would be: if ($_SERVER['SCRIPT_NAME'] === $thisPage)… May 16, 2026 at 4:52 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Does anyone know how can I replace this 2 symbol below from the string
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
In order to apply a triggered animation to all ToolTip s in my app,
I want use html5's new tag to play a wav file (currently only supported
I want to count how many characters a certain string has in PHP, but
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
Seemingly simple, but I cannot find anything relevant on the web. What is the

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.