Blog

Fetch Comma Separated Data using XML Path

Posted by Kunal Vaishya Blogs | SQL Oct 26, 2012
This is a good query to fetch comma separated data using XML Path.

Select Qry.*, Case When Qry.Door = '' Then Qry.Door Else Substring(Qry.Door, 3, Len(Qry.Door) - 2) End As Door

From (SELECT  dbo.tblHardware.Cmp_Id, Hardware_Id , Hardware_Code AS [Hardware Code],

(Select ', ' + cast(Door_Name as varchar)  FROM tbldoor

                                    WHERE tbldoor.Hardware_Id = tblHardware.Hardware_Id FOR XML path('')) Door

FROM  dbo.tblHardware WHERE (dbo.tblHardware.Is_Deleted = 0)) As Qry


Pleases refer below site for more detail

http://blog.sqlauthority.com/2009/11/25/sql-server-comma-separated-values-csv-from-table-column/

COMMENT USING
PREMIUM SPONSORS
Hire Mobile & Web Developer on demand. 100% satisfaction. Try for 1 week or Money Back. Local and remote developers available all over USA.
SPONSORED BY
  • MCN is your source for developing solutions involving websites, mobile apps, cloud-computing, databases, BI, back-end services and processes and client-server applications.
Employers - Post Free Jobs