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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:28:58+00:00 2026-06-14T19:28:58+00:00

I have looked through a number of solutions to emulating Group concat functionality in

  • 0

I have looked through a number of solutions to emulating “Group concat” functionality in SQL Server. I wanted to make a more human readable solution though and I can’t work out how to do it.

I have a view:

ParentID | ChildName

Which contains the records, for example:

1 | Max
1 | Jessie
2 | Steven
2 | Lucy
2 | Jake
3 | Mark

I want to “Group Concat” these to get:

1 | Max and Jessie
2 | Steven, Lucy and Jake
3 | Mark

So If there is only 1 child, just return name, if there are more than one, concat the last 2 with an ‘ and ‘ and all others with a ‘, ‘.

I am a bit stuck on how to do this without resorting to CLR, which I don’t want to do. I am happy with a function – but speed is an issue and how do I determine the child number so I can choose between ‘ and ‘, ‘, ‘ or ”?

  • 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-14T19:28:59+00:00Added an answer on June 14, 2026 at 7:28 pm

    make a more human readable solution

    Sorry, this is the best I can do with your requirement.

    SQL Fiddle

    MS SQL Server 2008 Schema Setup:

    create table YourTable
    (
      ParentID int,
      ChildName varchar(10)
    );
    
    insert into YourTable values
    (1, 'Max'),
    (1, 'Jessie'),
    (2, 'Steven'),
    (2, 'Lucy'),
    (2, 'Jake'),
    (3, 'Mark');
    

    Query 1:

    with T as 
    (
      select ParentID,
             ChildName,
             row_number() over(partition by ParentID order by ChildName) as rn,
             count(*) over(partition by ParentID) as cc
      from YourTable
    )
    select T1.ParentID,
           (
             select case
                      when T2.rn = 1 and T2.cc > 1 then ' and '
                      else ', ' 
                    end + T2.ChildName
             from T as T2
             where T1.ParentID = T2.ParentID
             order by T2.rn desc
             for xml path(''), type
           ).value('substring(text()[1], 3)', 'varchar(max)') as ChildNames
    from T as T1
    group by T1.ParentID
    

    Results:

    | PARENTID |            CHILDNAMES |
    ------------------------------------
    |        1 |        Max and Jessie |
    |        2 | Steven, Lucy and Jake |
    |        3 |                  Mark |
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have looked through the solutions on stackoverflow and none of them seem to
I have looked through the solutions and haven't really found one. I am getting
I have looked through a number of tutorials, but I still can't figure out
I have looked through this a number of times this morning and cannot find
Hi I am new to MVC and have looked through a great number of
I have looked through several sites for any useful documentation and have come up
I seem to have looked through many solution on here and the web and
I've downloaded the Windows Media Center SDK and have looked through the documentation, but
Ok.. I have looked through this site and just can't seem to find the
Composer is a PHP package manager. I have looked through the documentation here: http://getcomposer.org/

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.