Solved

Multiple Values in SQL table returned in single column separated by commas

Posted on 2013-11-12
3
350 Views
Last Modified: 2013-11-13
Hi Experts,

I have a SQL Server table of short (nvarchar 75) answers filed using a code to group them by. I'm wanting to run a query that will return all answers given for a specific code separated by a comma.

For example:
100    Apple
100    Orange
100    Pear
200    Grape
200    Tangerine

Query returns for group 100:
Apple, Orange, Pear

I've used FOR XML PATH in the past but was hoping to find a different approach. Thanks,

JackW9653
0
Comment
Question by:JackW9653
  • 2
3 Comments
 
LVL 23

Accepted Solution

by:
Rajkumar Gs earned 500 total points
Comment Utility
Hi
Using COALESCE . Refer this example
stackoverflow.com/questions/655197/converting-rows-into-columns-in-t-sql-sql-server-2005

Raj
0
 

Author Closing Comment

by:JackW9653
Comment Utility
Hi Raj,

Thanks for your reply. I used COALESCE and was getting extra characters between text and comma, then I added a RTRIM statement and the world was right again.

JackW9653
0
 
LVL 23

Expert Comment

by:Rajkumar Gs
Comment Utility
your are welcome
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now