Solved

SQL Results horizontal

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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL clustering failover 8 39
How come this XML node is not read? 3 25
sql server cross db update 2 20
Need help with another query 10 34
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
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
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

680 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