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

, 8 Mar 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
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:

Name
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

License

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

Share

About the Author

Ramanujam Shankar
Web Developer Mahindra Logisoft Business Solution Limited, Chenn
India India
No Biography provided

Comments and Discussions

 
QuestionThis is mostly... just... genius. PinmemberMember 15341609-Oct-14 7:50 
QuestionSELECT Query PinmemberKannan Karthikeyan6-Oct-14 11:16 
QuestionTo avoid the ", " at the beginning of the string PinmemberMember 101685651-Oct-13 0:47 
QuestionConcerned about the use of COALESCE vs a Case Function Pinmemberdalyteaching23-Aug-13 4:51 
GeneralMy vote of 5 PinmemberSk. Tajbir4-Feb-13 23:31 
Generalc# PinmemberMember 823748321-Dec-12 10:06 
Gracias
GeneralMy vote of 5 Pinmemberwolfcoder7514-Dec-12 10:53 
QuestionFor both sql server 2005 and 2008 PinmemberShawn02713-Mar-12 3:45 
AnswerRe: For both sql server 2005 and 2008 Pinmemberwolfcoder7514-Dec-12 10:58 
Questionbla bla Pinmembersajithinnate11-Mar-12 21:18 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.1411023.1 | Last Updated 8 Mar 2012
Article Copyright 2012 by Ramanujam Shankar
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid