Click here to Skip to main content
15,881,882 members
Articles / Database Development / SQL Server
Article

Get comma separated result

Rate me:
Please Sign up or sign in to vote.
1.18/5 (6 votes)
15 Nov 2007 24.6K   14   4
Create command for getting comma seperated value

Introduction

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:

DeptEmpName
Software Emp1
Software Emp2
Software Emp3
Account Emp4
Account Emp5

And I want output as below.

DeptEmpName
Software Emp1, Emp2, Emp3
Account Emp4, Emp5


//
// Code is shown below
//
SET ANSI_WARNINGS OFF
Declare @ono varchar(50)
DECLARE My_Cursor CURSOR FOR
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
WHILE @@FETCH_STATUS = 0
BEGIN
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
END
CLOSE My_Cursor
DEALLOCATE My_Cursor
select * from tmp

Points of Interest

Change anything as per your requirement. Happy coding.

License

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


Written By
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.....

Comments and Discussions

 
GeneralBetter Way to comma separated result Pin
sanjaymore14-Sep-09 1:01
sanjaymore14-Sep-09 1:01 
GeneralMy vote of 1 Pin
Manas Bhardwaj9-Jul-09 3:36
professionalManas Bhardwaj9-Jul-09 3:36 
GeneralI don't understand... Pin
Ashaman15-Nov-07 1:47
Ashaman15-Nov-07 1:47 
Are you saying that you already have a column with multiple employees listed in the same row as the department or that you want those results?

If it's a de-normalized DB structure, then I understand my confusion... otherwise, here's an alternative, below.

Let's say you have three tables: Department, Employee, Employee_Department

If you want employee names, separated by commas, for each department, then you could do something like this and not use a cursor or variables...

<br />
WITH EmpList AS<br />
(<br />
	SELECT DISTINCT DepartmentID, Employees<br />
	FROM Employee_Department ED<br />
	CROSS APPLY <br />
		(SELECT IE.FullName + ', '<br />
		FROM Employee_Department IED JOIN Employee IE ON IED.EmployeeID = IE.EmployeeID<br />
		WHERE IED.DepartmentID = ED.DepartmentID<br />
		FOR XML Path('')) as EmployeesCSV (Employees)<br />
)<br />
SELECT DepartmentName, Employees<br />
FROM Department E JOIN EmpList EL ON E.DepartmentID = EL.DepartmentID<br />

GeneralRe: I don't understand... Pin
Sun Rays15-Nov-07 18:08
Sun 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.