Click here to Skip to main content
Click here to Skip to main content

Get comma separated result

, 15 Nov 2007
Rate this:
Please Sign up or sign in to vote.
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:

Dept EmpName
Software Emp1
Software Emp2
Software Emp3
Account Emp4
Account Emp5

And I want output as below.

Dept EmpName
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

Share

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

Comments and Discussions

 
GeneralBetter Way to comma separated result Pinmembersanjaymore14-Sep-09 1:01 
GeneralMy vote of 1 PinmemberManas Bhardwaj9-Jul-09 3:36 
GeneralI don't understand... PinmemberAshaman15-Nov-07 1:47 
GeneralRe: I don't understand... PinmemberSun Rays15-Nov-07 18:08 

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 | Mobile
Web02 | 2.8.141022.2 | Last Updated 15 Nov 2007
Article Copyright 2007 by Sun Rays
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid