Solved

SQL Results horizontal

Posted on 2011-09-26
2
380 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

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

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

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
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 backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

739 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