Click here to Skip to main content
13,055,047 members (74,000 online)
Click here to Skip to main content
Add your own
alternative version


14 bookmarked
Posted 15 Nov 2007

Get comma separated result

, 15 Nov 2007
Rate this:
Please Sign up or sign in to vote.
Create command for getting comma seperated value


Here is some help for people who are awaiting for output from Sql server command described as below. As we are having one table named as MyTable in which we are having two columns named as Dept which contains name of all department within company and another column named as EmpName contains name of all the employess working in the company related to that department.

Now, with the store procedure we are getting all the department name in first column and in second column we are having all the employee names comma separated. So with the below code we will get the same out put.

Using the code

I have table name : MyTable.

Columns : Dept, EmpName

I have table output like below:

Software Emp1
Software Emp2
Software Emp3
Account Emp4
Account Emp5

And I want output as below.

Software Emp1, Emp2, Emp3
Account Emp4, Emp5

// Code is shown below
Declare @ono varchar(50)
SELECT distinct Dept
FROM MyTable
drop table tmp
create table tmp(ono varchar(50),allname varchar(50))
OPEN My_Cursor
FETCH NEXT FROM My_Cursor into @ono
DECLARE @EmployeeList varchar(100)
set @EmployeeList = null
SELECT  @EmployeeList = COALESCE(@EmployeeList + ', ', '') + 
   CAST(EmpName AS varchar(5))
FROM MyTable where Dept = @ono
insert into tmp(ono,allname) values(@ono,@EmployeeList)
    FETCH NEXT FROM My_Cursor into @ono
CLOSE My_Cursor
select * from tmp

Points of Interest

Change anything as per your requirement. Happy coding.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Sun Rays
Team Leader
India India
Working as a Team Lead in MNC located at India.
In IT world, I have worked with Microsoft Technologies and always ready for R&D.
Worked with Website Development and Windows based applications as well.

New errors are really good and being happy to resolve those.....

You may also be interested in...


Comments and Discussions

GeneralBetter Way to comma separated result Pin
sanjaymore14-Sep-09 1:01
membersanjaymore14-Sep-09 1:01 
GeneralMy vote of 1 Pin
Manas Bhardwaj9-Jul-09 3:36
memberManas Bhardwaj9-Jul-09 3:36 
GeneralI don't understand... Pin
Ashaman15-Nov-07 1:47
memberAshaman15-Nov-07 1:47 
GeneralRe: I don't understand... Pin
Sun Rays15-Nov-07 18:08
memberSun Rays15-Nov-07 18:08 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170713.1 | Last Updated 15 Nov 2007
Article Copyright 2007 by Sun Rays
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid