question

Murali avatar image
Murali asked

how to concatenate row value in a column

NO NAME COMPANY SCHEME 1 JOHN TATA TATA1 2 JOHN TATA TATA2 3 JOHN BIRLA BIRLA1 4 JOHN BIRLA BIRLA2 i am getting an output of the query like above, but i need to get .. as customer name is same and company is same but only scheme is differ, instead of getting two rows i need to fetch only one row..in that scheme as to come by , separated ... like NO NAME COMPANY SCHEME 1 JOHN TATA TATA1,TATA2
t-sql
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

rich76 avatar image
rich76 answered
1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Murali avatar image Murali commented ·
it is really really a very good article ..thanks alot
0 Likes 0 ·
WilliamD avatar image
WilliamD answered
Murali, you didn't mention the version of SQL Server, but this will work for 2005 and newer: WITH TestData([NO], NAME, COMPANY) AS (SELECT 1, 'JOHN TATA', 'TATA1' UNION SELECT 2, 'JOHN TATA', 'TATA2' UNION SELECT 3, 'JOHN BIRLA', 'BIRLA1' UNION SELECT 4, 'JOHN BIRLA', 'BIRLA2') SELECT Name,Companies = STUFF ((SELECT ',' + Company FROM TestData c WHERE c.NAME = t.NAME FOR XML PATH ('')),1,1,'') FROM TestData t GROUP BY NAME
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.