Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / database / SQL-Server / SQL-Server-2008

Concatenate many rows into a single text string using SQL Server 2008

5.00/5 (1 vote)
25 Feb 2012CPOL 20.6K  
How to concatenate many rows into a single text string using SQL Server 2008.
Greetings,

try this:

SQL
if object_id('people') is Not Null drop view people;
GO
create view people ( Name ) 
as
select 'RAM'
union all select 'GURU'
union all select 'Sundar'
union all select 'Shyam'
union all select 'Inba'
union all select 'Kalai'
GO
select replace( replace( replace( (select Name as R from People for XML raw), '"/><row r="', ', ' ), '">', '' ), '</row>

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)