How to Concatenate Column Values from Multiple Rows into a Single Column with SQL Server

Concatenate Column Values from Multiple Rows into a Single Column with SQL Server

I want to concat a field's values from multiple records in a single field. For example:

Name
--------
Joseph
Paul
Mohan

Result should look like:

Names
---------
Joseph, Paul, Mohan

Thanks in advance
LVL 1
K_CH_KrishnaAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Rajkumar GsConnect With a Mentor Software EngineerCommented:
This will do

Raj
SELECT   
  NULLIF(  
    STUFF(  
      (SELECT ', ' + Name FROM YourTable FOR XML PATH('') 
                        ), 1, 2, ''  
    ), ''  
  ) AS AllNames

Open in new window

0
 
krunal_shahCommented:
check this,
USE AdventureWorks
GO
-- Check Table Column
SELECT Name
FROM HumanResources.Shift
GO
-- Get CSV values
SELECT SUBSTRING(
(SELECT ',' + s.Name
FROM HumanResources.Shift s
ORDER BY s.Name
FOR XML PATH('')),2,200000) AS CSV
GO


http://blog.sqlauthority.com/2009/11/25/sql-server-comma-separated-values-csv-from-table-column/
0
 
K_CH_KrishnaAuthor Commented:
Solution is perfect. It works
0
 
K_CH_KrishnaAuthor Commented:
How can include where condition here ?
for example if i want to display only names which starts with 'J'
0
All Courses

From novice to tech pro — start learning today.