Home > SQL

Using COALESCE function to concatenate column data in sql query

16. August 2008

May time we need column's data into a single row from a database table. This is easy to do if we are doing this at application level. However this could also be done in a sql query as well.

For example, suppose we have a table for "Employees":

SELECT * FROM Employees

EmpId      EmployeeName
-----------------------
1001         Susan
1002         Waqas
1003         Amjad
1004         Naveed
1005         David

 

Now I want to show all employee names in a single row separated by comma (,). One commonly used approach is to fetch all rows and concatenate data into out application

while (dr.Read())
{
  EmployeeNames += dr["EmployeeName"] + ",";
}

 

We can do the same thing in a Sql Server Query also

DECLARE @EmployeeName VARCHAR(8000) 
SELECT @EmployeeName = COALESCE(@EmployeeName + ', ', '') + EmployeeName FROM Employees
SELECT EmployeeName = @EmployeeName

EmployeeName
------------
Susan, Waqas, Amjad, Naveed, David

 

The COALESCE function is used to ensure that there is no comma (,) after the last EmployeeName.

SQL

Comments

3/26/2010 3:39:13 AM #
I don't agree with everything in this blog, but you do make some very good points. Im very interested in this subject and I myself do alot of research as well. Either way it was a well thoughtout and nice read so I figured I would leave you a comment. Feel free to check out my website sometime and let me know what you think.
4/22/2010 10:38:58 PM #
You should truly alter the comments here
4/23/2010 5:17:41 AM #
Terrific site, where did you come up with the knowledge in this blog post? I'm pleased I found it though, ill be checking back soon to see what other articles you have.
4/28/2010 5:45:10 AM #
i know this is not exactly on topic, but i have a blog using the blogengine platform as well and i'm having troubles with my comments displaying. is there a setting i am forgetting? maybe you could help me out? thank you.
6/8/2010 7:42:22 AM #
Did you know that I came to your website from the first page of aYahoo seach. Great Job. I know how nerve raking it is to get your your website on the first page of a search. Ive downloaded program after program and now got listen to music online for free without downloading</SPAN></SPAN></SPAN> on the first page. thank gosh only took 3weeks!!
6/12/2010 4:07:48 AM #
This World Cup should be really intense!  It comes down to flip of a coin in my opinion but good luck to both soccer teams! I'll be watching for sure!
6/14/2010 12:17:10 AM #
This year the final FIFA match could be very close!   I give it a coin toss with my opinion but good luck to both soccer teams! I will be watching the entire match!
6/25/2010 10:37:28 AM #
Good post, good info, just top notch. Linked you back.
7/8/2010 9:30:38 AM #
This is the perfect blog for anyone who wants to know about this topic.  You know so much its almost hard to argue with you (not that I really would want...HaHa).  You definitely put a new spin on a subject thats been written about for years.  Great stuff, just great!
9/4/2010 7:57:55 AM #
I have took note of all the content you've  on this website and have found it quite informative
9/9/2010 10:32:15 PM #
I must say, as much as I loved reading what you had to say, I couldnt assist however lose curiosity after a while. Its as in case you had a terrific grasp on the subject material, but you forgot to incorporate your readers. Maybe you must take into consideration this from multiple angle. Or maybe you shouldnt generalise so much. Its better if you consider what others may should say as a substitute of just going for a intestine response to the subject. Think about adjusting your individual thought process and giving others who could read this the advantage of the doubt.
9/10/2010 12:13:11 AM #
I need to say, as a lot as I loved reading what you needed to say, I couldnt assist but lose curiosity after a while. Its as if you had a terrific grasp on the subject material, but you forgot to include your readers. Perhaps it's best to take into consideration this from multiple angle. Or maybe you shouldnt generalise so much. Its higher if you consider what others could must say as a substitute of simply going for a gut response to the subject. Think about adjusting your own thought process and giving others who might read this the advantage of the doubt.

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading