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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T16:52:42+00:00 2026-06-01T16:52:42+00:00

Possible Duplicate: Simulating group_concat MySQL function in MS SQL Server 2005? SQL Query for

  • 0

Possible Duplicate:
Simulating group_concat MySQL function in MS SQL Server 2005?
SQL Query for aggregation/concatenation

I am trying to use the stuff function in SQL server to stuff certain info. Here is the example:

Money     Age    Gender
860       9          F
860       15         M
860       15         M
860       16         M
860       16         F

I would like to stuff the Age and Gender column so that only one record will display as following:

Money   Age                 Gender
860     9, 15, 15, 16, 16   F, M, M, M, F

Please note, I would like to keep the two 15s and three M in the Age and Gender respectively.

  • 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-01T16:52:43+00:00Added an answer on June 1, 2026 at 4:52 pm

    This is easier to do using a FOR XML rather than stuff. Keep in mind that the FORM XML clause can be tricked to generate a comma seperated list (CSV).

    The example below should do exactly what you are asking for.

    CREATE TABLE moneyagegender 
    ( 
     [Money]  INT, 
     [Age]    INT, 
     [Gender] VARCHAR(2) 
    ); 
    
    INSERT INTO moneyagegender 
    VALUES      (860, 9, 'F'), 
            (860, 15, 'M'), 
            (860, 15, 'M'), 
            (860, 16, 'M'), 
            (860, 16, 'F'); 
    
    SELECT mag.money, 
       (SELECT Substring((SELECT ', ' + CAST(m2.age AS VARCHAR(1024)) 
                          FROM   moneyagegender m2 
                          WHERE  m2.money = mag.money 
                          ORDER  BY m2.age 
                          FOR XML PATH('')), 3, 10000000) AS list) AS ages, 
       (SELECT Substring((SELECT ', ' + m3.gender 
                          FROM   moneyagegender m3 
                          WHERE  m3.money = mag.money 
                          ORDER  BY m3.age 
                          FOR XML PATH('')), 3, 10000000) AS list) AS genders 
    FROM   moneyagegender mag 
    GROUP  BY mag.money; 
    

    and here is the output.

    Money       Ages                 Genders
    ----------- -------------------- -----------------
    860         9, 15, 15, 16, 16    F, M, M, M, F
    (1 row(s) affected)
    

    I hope this helps.

    If you need more details, I have a blog posting from last year that explains this.
    http://stevestedman.com/2011/10/converting-part-of-a-result-set-to-a-comma-separated-list/

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

Sidebar

Related Questions

Possible Duplicates: Simulating group_concat MySQL function in MS SQL Server 2005? Concat groups in
Possible Duplicate: Simulating a BlueScreen Hello SO, I'm trying to induce a BSOD somehow
Possible Duplicate: SQL Server UDF refresh The system stored procedure sp_refreshview can be used
Possible Duplicate: Yield In VB.NET In C#, when writing a function that returns an
Possible Duplicate: Best way to stop SQL Injection in PHP I am creating a
Possible Duplicate: Finding the Variable Name passed to a Function in C# In C#,
Possible Duplicate: PHP get all arguments as array? Within a javascript function arguments always
Possible Duplicate: regex for URL including query string I have a text or message.
Possible Duplicate: How to call a JavaScript function from PHP? I have a php
Possible Duplicate: What is the difference between a function expression vs declaration in JavaScript?

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.