Solved

SQL Results horizontal

Posted on 2011-09-26
2
383 Views
Last Modified: 2012-05-12
I found this sample online:

DECLARE @id_list VARCHAR(MAX) --varchar max works for SQL 2005 or above
 
SELECT @id_list = CASE WHEN @id_list IS NULL THEN CONVERT(VARCHAR,column1)
            ELSE @id_list + ',' + CONVERT(VARCHAR,column1) END
            FROM table1            
SELECT @id_list



which works great to display a single column horizontally but I need to display one column normally with the results of another horizontally:

Bob smith  |  09/12/2011, 09/13/2011, 09/15/2011    

in that example all dates with the name value bob smith.
0
Comment
Question by:RustyZ32
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 22

Accepted Solution

by:
pivar earned 500 total points
ID: 36601805
Hi,

You could use FOR XML


SELECT Name, STUFF((SELECT ', '+CONVERT(varchar, t2.Date, 101) AS[text()] FROM table1 t2 WHERE t2.Name=t1.Name FOR XML PATH('')), 1, 2, '') AS list
FROM table1 t1
GROUP BY t1.Name


/peter
0
 
LVL 6

Author Closing Comment

by:RustyZ32
ID: 36601834
worked great! thanks.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how the fundamental information of how to create a table.

626 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