Wednesday 14 January 2015

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

Using COALESCE function - concatenate many rows into a single text string output in SQL Server.

The following code is used to concatenate many rows into a single text string with comma separated using SQL Server COALESCE function.

My table format is like this:
RAM
GURU
Sundar
Shyam
Inba
Kalai
I need output like this:
"RAM, GURU, Sundar, Shyam, Inba, Kalai"

I use the following query:
DECLARE @Names VARCHAR(8000)  
SELECT @Names = COALESCE(@Names + ', ', '') + Name FROM People
SELECT @Names


---------------------------------------------------------------------------------
Come from: http://www.codeproject.com/Tips/334400/Concatenate-many-rows-into-a-single-text-string-us

No comments:

Post a Comment