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

  • Home
  • SEARCH
  • 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 8613201
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T04:48:53+00:00 2026-06-12T04:48:53+00:00

Possible Duplicate: SQL Server: Can I Comma Delimit Multiple Rows Into One Column? SQL

  • 0

Possible Duplicate:
SQL Server: Can I Comma Delimit Multiple Rows Into One Column?
SQL Query to get aggregated result in comma seperators along with group by column in SQL Server

I have a table similar to this:

DocumentID      Name
1           Jack D'Souza
1           Rick Astley
1           Frankestein
1           Einstein

I want to write a SQL Server scalar function which would accept in parameter as DocumentID and give a CSV separated value. So, If I pass in 1, it would give Jack D'Souza, Rick Astley, Frankestein, Einstein

Inside the function, I can create in cursor and create this CSV list. But is there any alternative to using cursor?

  • 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-12T04:48:55+00:00Added an answer on June 12, 2026 at 4:48 am

    Have a look at this example

    CREATE FUNCTION MyConcat(@ID INT)
    RETURNS VARCHAR(MAX)
    AS
    BEGIN
      DECLARE @RetVal VARCHAR(MAX)
        SELECT  @RetVal = stuff(
                        (
                            select  ',' + t1.Name
                            from    MyTable t1
                            where   t1.DocumentID = t.DocumentID
                            for xml path('')
                        ),1,1,'') 
        FROM    MyTable t
        WHERE   t.DocumentID = @ID
        GROUP BY t.DocumentID
      RETURN @RetVal
    END;
    

    SQL Fiddle DEMO

    You could also create a table function that will return all the values that you need.

    Something like

    CREATE FUNCTION MyConcatTable()
    RETURNS @RetTable TABLE(
        [DocumentID] int, 
        [Name] varchar(MAX)
    )
    AS
    BEGIN
        INSERT INTO @RetTable
        SELECT  t.DocumentID,
                stuff(
                        (
                            select  ',' + t1.Name
                            from    MyTable t1
                            where   t1.DocumentID = t.DocumentID
                            for xml path('')
                        ),1,1,'') 
        FROM    MyTable t
        GROUP BY t.DocumentID
      RETURN
    END;
    

    SQL Fiddle DEMO

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

Sidebar

Related Questions

Possible Duplicate: SQL Server: Can I Comma Delimit Multiple Rows Into One Column? I
Possible Duplicate: SQL Server: Can I Comma Delimit Multiple Rows Into One Column? I
Possible Duplicate: SQL Server: Can I Comma Delimit Multiple Rows Into One Column? Script
Possible Duplicate: SQL Server: Can I Comma Delimit Multiple Rows Into One Column? I
Possible Duplicate: SQL Server: Can I Comma Delimit Multiple Rows Into One Column? I
Possible Duplicate: Selecting data from two different servers in SQL Server How can I
Possible Duplicate: How can I determine installed SQL Server instances and their versions? i've
Possible Duplicate: SQL Server Database query help Hi, I have a problem that I
Possible Duplicate: Can a number used to name a sql column I am trying
Possible Duplicate: SQL Server UDF refresh The system stored procedure sp_refreshview can be used

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.