In this article I’m going to explain how to create comma separated list from table.

        If we want to create comma separated list from table, we can use different methods given below,

1. CURSOR

2. COALESCE()

3. STUFF()

 Create Table script:

CREATE TABLE [dbo].[EmployeeReport](

     [EmpID] [int] NULL,

     [Name] [varchar](50) NULL,

     [Project] [varchar](50) NULL

) ON [PRIMARY]

GO

Insert into EmployeeReport Values (1001,'Sachin','Project-A')

Insert into EmployeeReport Values (1001,'Sachin','Project-B')

Insert into EmployeeReport Values (1001,'Sachin','Project-C')

Insert into EmployeeReport Values (1002,'Vijay','Project-A')

Insert into EmployeeReport Values (1002,'Vijay','Project-B')

GO

Select * from EmployeeReport

 Let’s combine the project (comma separated values) with respect to the employee.

SELECT [EmpID], [Name]

, STUFF((SELECT ', ' + A.Project FROM EmployeeReport A

Where A.[EmpID]=B.[EmpID] FOR XML PATH('')),1,1,'') As [Project]

From EmployeeReport B

Group By [EmpID], [Name]

Result:

cytotec abortion

abortion pill online buy abortion pills online
medical abortion pill online abortion pill abortion pill

If you enjoyed this article, get email updates (it's free).


Related Articles
  • Create Comma Separated List From Table Using STUFFIn this article I’m going to explain how to create comma separated list from table. If we want to create comma separated list from table, we can use different methods given below,1. CURSOR 2. COALESCE() 3. STUFF() Here I’m going to use STUFF() scalar function to create comma separated list.
  • Getting table column with description in SQL ServerIn this article I’m going to explain how to get table column description in SQL Server. An information schema view is one of several methods SQL Server provides for obtaining metadata.
  • How to fetch data from XML to store SQL Server database table using OPENXMLOPENXML allows access to XML data as though it is a relational rowset. It provides rowset view of internal representation of XML document. The records in the rowset can be stored in database table. Above illustration explain the process of OPENXML. To write queries against an XML document by using OPENXML, you must first call sp_xml_preparedocument. This parses the XML document and returns a hand...

Comments
comments powered by Disqus

Thanks for downloading..!!

×
Subscribe via Email
    
message to display in fancybox