Click here to Skip to main content
15,891,905 members
Articles / Database Development / SQL Server / SQL Server 2008
Tip/Trick

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

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
25 Feb 2012CPOL 19.9K   1   4
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)


Written By
Brazil Brazil
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralReason for my vote of 5 nice one Pin
Nikhil_S26-Feb-12 17:45
professionalNikhil_S26-Feb-12 17:45 
Generalif object_id('people') is Not Null drop view people; GO crea... Pin
pandiyarajk26-Feb-12 7:49
pandiyarajk26-Feb-12 7:49 
GeneralThat doesn't really do the same job - the original concatena... Pin
OriginalGriff22-Feb-12 23:22
mveOriginalGriff22-Feb-12 23:22 
GeneralRe: The vision I just used to illustrate, change the view by the... Pin
e-Lopes25-Feb-12 13:25
e-Lopes25-Feb-12 13:25 
The vision I just used to illustrate, change the view by the original table.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.