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 single column with multiple rows to

Author  Topic 

raghav99k
Starting Member

32 Posts

Posted - 2011-11-17 : 01:30:35
Hi,

i want to display single column with multiple rows to a singl column..is there any way to acheive this using sql query..
Select ID From Temp

ID
1
2
3
4
5
6


o/p should look like
ID
1 2 3 4 5 6

Thanks in Advance..

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-11-17 : 02:06:50
[code]
SELECT STUFF((SELECT ' ' + CAST(ID AS varchar(10)) FROM table ORDER BY ID FOR XML PATH('')),1,1,'')
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

raghav99k
Starting Member

32 Posts

Posted - 2011-11-17 : 02:13:16
Thanks Visakh..U r really awesome.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-11-17 : 03:59:54
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

Sachin.Nand

2937 Posts

Posted - 2011-11-17 : 04:22:25
Hmm why do you need XML PATH for this ?


declare @t table(id int)
insert @t
select 1 union all
select 2 union all
select 3 union all
select 4

declare @id varchar(10)=''
select @id=@id +',' + convert(varchar(10),ID) from @t
select stuff(@id, 1,1,'')


PBUH

Go to Top of Page
   

- Advertisement -