Database Simplified Headline Animator

Database Simplified

Wednesday 9 November 2011

How To Get Comma Separated Value In SQL Server.

In the earlier post we have seen use of xml path to get comma separated result. This post describes how to get comma separated value from a result set or a table data.

Create Table With Following Script.



Create Table CSData
(
Id Int Identity(1,1),
DepartmentName Varchar(40)
)
Go

Insert Into CSData(DepartmentName) Values('IT'),('FINANCE'),('MEDICAL'),('TRANSPORT'),('TALENT TRANSFORMATION'),('IMG')
Go

You have following data.

image

Now we need comma separated departmentnames.

image

So its quite easy to have comma separated list. I have used stuff to removed first comma from the result.

5 comments: