How to create Comma Separate Values List from Table in SQL Server

Comma Separate Values List 

Before going to below tutorial Download adventureworks database for sql server 2008 r2.

Below query shows, how to create comma separated list using COALESCE keyword.

declare @_MonthDay varchar(max)

Select @_MonthDay=COALESCE(@_MonthDay+']'+',[' , '[') + 
datename(mm,HireDate)+ ' '+cast(day(HireDate) as varchar) 
from [AdventureWorks2008R2].[HumanResources].[Employee] 
WHERE HireDate BETWEEN --@fromDate AND @toDate 
'2003-01-15' and '2003-01-20'
Group by 
 --year(HireDate),
 month(HireDate),
 day(hiredate),
 datename(mm,HireDate)
set @_MonthDay =@_MonthDay+']'
select @_MonthDay


Output:

How to create Comma Separate Values List from Table in SQL Server
How to create Comma Separate Values List from Table in SQL Server



Share this post :

Post a Comment

Please give your valuable feedback on this post. You can submit any ASP.NET article here. We will post that article in this website by your name.

 
Support : Ranga Rajesh Kumar
Copyright © 2012. ASP.NET Examples - All Rights Reserved
Site Designed by Ranga Rajesh Kumar