How to concatenate a field from multiple rows into a single text string in SQL Server?

SQL Buddy
SQL Buddy
2 Points
1 Posts

How to concatenate a field from multiple rows into a single text string in SQL Server?

Consider a database table 'User' holding names, with three rows as:

  1. Peter
  2. Paul
  3. Mary

I want query result as comma separated string as 

Peter,Paul,Mary

Views: 67
Total Answered: 1
Total Marked As Answer: 0
Posted On: 23-Jan-2023 22:57

Share:   fb twitter linkedin
Answers
Rahul Maurya
Rahul M...
Teacher
1416 Points
238 Posts
         

Use FOR XML PATH clause as:

SELECT Name +',' FROM User FOR XML PATH('')
Posted On: 26-Jan-2023 05:29
 Log In to Chat