Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 How to display many rows data to a single row.

Author  Topic 

pvinesh
Starting Member

3 Posts

Posted - 2008-03-14 : 06:35:09
I have a table with only one column as

Emp_Name
A
B
C
D
E

I want to display this data as

Emp_Name
A, B, C, D, E

Is this possible? I am using SQL server 2005.
Any help is appreciated.

Regards,
Vinesh

ranganath
Posting Yak Master

209 Posts

Posted - 2008-03-14 : 06:49:16
Hi,

Try with this

deCLARE @StrConcat tABLE (col1 nvarchar(10),col2 nvarchar(10))

insert into @StrConcat
select 'db1','host1'
union all select 'db1','host2'
union all select 'db1','host3'
union all select 'db2','host1'
union all select 'db2','host2'
union all select 'db3','host2'
union all select 'db3','host3'


select col1, stuff( ( select ','+ col2 from @StrConcat t1 where t2.col1 = t1.col1 for xml path('')),1,1,'')
from @StrConcat t2
group by col1
order by col1
Go to Top of Page

pvinesh
Starting Member

3 Posts

Posted - 2008-03-14 : 07:02:34
Thanks for your reply!

Could you please eloborate it more as I am new to sql server.

Regards,
Vinesh
Go to Top of Page
   

- Advertisement -