Tuesday, February 26, 2013

Select Column Comma Separated in SQL Server

-Example 1:

SELECT top 1
              STUFF ( ( SELECT ','+inTab.FIRST_NAME
                           FROM  crm.CONSUMERS inTab
                              FOR XML PATH(''),TYPE).value('.','VARCHAR(MAX)'),1 ,1,SPACE(0)) as ConsumersListWithComms
FROM crm.CONSUMERS OutTab


--Example 2:

SELECT TOP 1 (SELECT inTab.FIRST_NAME+','
                     FROM CRM.CONSUMERS inTab FOR XML PATH(''))
                     AS ConsumersListWithComms
FROM CRM.CONSUMERS outTab

No comments: