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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T20:25:04+00:00 2026-06-02T20:25:04+00:00

I have a data set that looks something like this: Gender | Age |

  • 0

I have a data set that looks something like this:

   Gender | Age | Name
    Male  | 30  | Bill
  Female  | 27  | Jenny
  Female  | 27  | Debby 
   Male   | 44  | Frank

And I’m trying to display this as specially formatted HTML code:

    <ul>
      <li>Male
        <ul>
          <li>30
            <ul>
              <li>Bill</li>
            </ul>
          </li>
          <li>44
            <ul>
              <li>Frank</li>
            </ul>
          </li>
        </ul>  
      </li>
    </ul>

    <ul>
      <li>Female
        <ul>
          <li>27
            <ul>
              <li>Jenny</li>
              <li>Debby</li>
            </ul>
          </li>
        </ul>  
      </li>
    </ul>

I tried using FOR XML but that didn’t give the results I was looking for. It didn’t remove the multiple Gender and Age fields returned. As you can see in this HTML it is compounding it all and only giving duplicates at the end node.

How would something like this be achieved in SQL Server?

  • 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-02T20:25:06+00:00Added an answer on June 2, 2026 at 8:25 pm

    Here is a really ugly way formulating the HTML manually. There is a good reason this doesn’t belong in SQL Server. I’m sure some XML guru will come along and embarrass me with a much more straightforward method (I played with Simon Sabin’s solution but couldn’t translate it to your requirement), but for now:

    DECLARE @x TABLE(Gender VARCHAR(6), Age INT, Name VARCHAR(32));
    
    INSERT @x VALUES  ('Male',   30, 'Bill'),  ('Female', 27, 'Jenny'),
                      ('Female', 27, 'Debby'), ('Male',   44, 'Frank');
    
    DECLARE @html NVARCHAR(MAX) = N'';
    
    ;WITH x AS ( SELECT x.Age, x.Gender, x.Name,
        dr = DENSE_RANK() OVER (PARTITION BY x.Gender ORDER BY x.Age),
        gn = ROW_NUMBER() OVER (PARTITION BY x.Gender ORDER BY x.Age),
        rn = ROW_NUMBER() OVER (ORDER BY x.Gender DESC, x.Age)
      FROM @x AS x ) SELECT @html +=
        CHAR(13) + CHAR(10) + CASE WHEN c1.gn = 1 THEN 
            CASE WHEN c1.rn > 1 THEN '</li></ul></li></ul>' ELSE '' END + '<ul><li>' 
            + c1.Gender ELSE '' END + CHAR(13) + CHAR(10) + CHAR(9) 
            + CASE WHEN c1.gn = 1 OR c1.Age <> c3.Age THEN 
            CASE WHEN c1.gn > 1 THEN '</li>' ELSE '<ul>' END + '<li>' 
            + CONVERT(VARCHAR(32), c1.Age) ELSE '' END + CHAR(13) + CHAR(10) + CHAR(9) 
            + CHAR(9) + CASE WHEN (c1.gn = 1 OR c1.Age <> c3.Age) THEN '<ul>' ELSE '' END 
            + '<li>' + c1.Name + '</li>' + CASE WHEN c1.Age <> c2.Age OR c1.dr <> c2.dr 
            THEN '</ul>' ELSE '' END
    FROM x AS c1 
    LEFT OUTER JOIN x AS c2
    ON c1.rn = c2.rn - 1
    LEFT OUTER JOIN x AS c3
    ON c1.rn = c3.rn + 1
    ORDER BY c1.Gender DESC, c1.Age;
    
    SELECT @html += '</ul></li></ul></li></ul>';
    
    PRINT @html; -- note you will need to deal with this 
                 -- in another way if the string is large
    

    Result – not exactly what you asked for in terms of white space, but identical HTML rendering:

    <ul><li>Male
        <ul><li>30
            <ul><li>Bill</li></ul>
    
        </li><li>44
            <ul><li>Frank</li></ul>
    </li></ul></li></ul><ul><li>Female
        <ul><li>27
            <ul><li>Jenny</li>
    
    
            <li>Debby</li></ul></li></ul></li></ul>
    

    EDIT For a much cleaner solution, as well as a lot of drama and a good demonstration of why @ZeeTee is the most annoying user on StackOverflow, see Mikael’s solution to the follow-up question:

    Return Select Statement as formatted HTML (SQL 2005)

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

Sidebar

Related Questions

I have a data set that looks something like this: Gender | Age |
I have a KnockoutJS template that looks something like this: <div data-bind=template: {name: 'testTemplate',
I have data that always looks something like this: alt text http://michaelfogleman.com/static/images/chart.png I need
I have a huge data set. The structure looks something like this: K_Field1, K_Field2,
I have a set of data that is structured like this: ItemA.GroupA ItemB.GroupA ItemC.GroupB
I have a base Color class that looks something like this. The class is
I have html that looks something like this: <div> <table> <tr onclick=show();> <td class=cell>Click
I have an Oracle table with data that looks like this: ID BATCH STATUS
Let's say I have a manager that looks something like this: public class CustomerManager
I have a data set that looks like the below (the input). IR# CR#

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.