Click here to Skip to main content
Click here to Skip to main content

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

By , 25 Feb 2012
 
Greetings,
 
try this:
 
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)

About the Author

e-Lopes
Brazil Brazil
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralReason for my vote of 5 nice onemembernikhi _singh26 Feb '12 - 17:45 
Reason for my vote of 5
nice one
Generalif object_id('people') is Not Null drop view people; GO crea...grouppandiyarajk26 Feb '12 - 7:49 
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), '"/>', '' ), ',','')
GeneralThat doesn't really do the same job - the original concatena...mvpOriginalGriff22 Feb '12 - 23:22 
That doesn't really do the same job - the original concatenates a single column content from all the rows in the table.
Yours uses just fixed text...
GeneralRe: The vision I just used to illustrate, change the view by the...membere-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    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130516.1 | Last Updated 25 Feb 2012
Article Copyright 2012 by e-Lopes
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid