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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:02:04+00:00 2026-05-26T08:02:04+00:00

I’ve searched many threads, sites, and blogs, as well as the API and can’t

  • 0

I’ve searched many threads, sites, and blogs, as well as the API and can’t seem to figure this one out. I want to create a CSV string from rows in a junction table, but here’s the catch. It needs to be for an entire set of data. I know, confusing, here’s the breakdown.

Let’s use the generic example with the following tables:

  • Students (ID, Name, Gpa)
  • Classes (ID, Dept, Level)
  • StudentsInClasses (StudentID, ClassID)

Say I want to query for all classes, but within that query (for each record) I want to also get a CSV string of the Student IDs in each of the classes. The results should look something like this:

Class      StudentsID_CSVString
-----      --------------------
BA302      1,2,3,4,5,6,7,8,9
BA479      4,7,9,12,15
BA483      7,9,12,18

I tried to use a coalesce statement like the following, but I can’t get it to work because of the goofy syntax for coalesce. Is there a better way to accomplish this goal or am I just making a dumb syntactical error?

declare @StudentsID_CSVString varchar(128)
select Dept + Level as 'Class',
    coalesce(@StudentsID_CSVString + ',', '')
        + CAST(StudentID as varchar(8)) as 'StudentsID_CSVString'
from Classes
    left outer join StudentsInClasses ON Classes.ID = StudentsInClasses.ClassID

I used the following code to test:

declare @Students table (ID int, Name varchar(50), Gpa decimal(3,2))
declare @Classes table (ID int, Dept varchar(4), [Level] varchar(3))
declare @StudentsInClasses table (StudentID int, ClassID int)
declare @StudentsID_CSVString varchar(128)

insert into @Students (ID) values (1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11),(12),(13),(14),(15),(16),(17),(18)
insert into @Classes (ID, Dept, [Level]) values (1, 'BA', '302'), (2, 'BA', '379'), (3, 'BA', '483')
insert into @StudentsInClasses (StudentID, ClassID) values
    (1,1),(2,1),(3,1),(4,1),(5,1),(6,1),(7,1),(8,1),(9,1),
    (4,2),(7,2),(9,2),(12,2),(15,2),
    (7,3),(9,3),(12,3),(18,3)
  • 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-26T08:02:05+00:00Added an answer on May 26, 2026 at 8:02 am

    With the technique to fill a variable @StudentsID_CSVString with a comma separated string you will only be able to get the string for one class at a time. To get more than one class in the result set you can use for xml path.

    select C.Dept+C.Level as Class,
           stuff((select ','+cast(S.StudentID as varchar(10)) 
                  from StudentsInClasses as S
                  where S.ClassID = C.ID
                  for xml path('')), 1, 1, '') as StudentsID_CSVString
    from Classes as C
    

    Test the query here: https://data.stackexchange.com/stackoverflow/q/115573/

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

Sidebar

Related Questions

I want to count how many characters a certain string has in PHP, but
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm making a simple page using Google Maps API 3. My first. One marker
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,

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.