Concatenation of Columns into Rows in SQL Server

Niladri.Biswas
Posted by Niladri.Biswas under Sql Server category on | Points: 40 | Views : 871
I need a query to convert the particular columns into single column.

Eg.
sno name
1 AA
2 BB
3 CC
4 DD
5 EE

output

name
AA,BB,CC,DD,EE

Solution

Declare @t table(SNo int identity,name varchar(50))
Insert Into @t
select 'AA' Union All select 'BB' Union All select 'CC' Union All
select 'DD' Union All select 'EE'

Select Name = stuff((Select ',' + CAST(name as varchar(100))
From @t
for xml path('')),1,1,'')

Comments or Responses

Login to post response