Text Concatenation in SQL Server

Sunday, 17 May 2009

To concate the Text for a combination of keys there might be many ways.

Initially I was using either function or cursor for concatenation. But finally I found the below query most useful for the same.

First Create a table called Product with the following data.


Don't forget to create index if your table has huge data.

Then run the below Query:

SELECT
DISTINCT T.ID
,(SELECT ' ' + [DESC] FROM PRODUCT WHERE ID = T.ID FOR XML PATH('')) AS [DESC] FROM PRODUCT T

You will get the output as:



I hope this will help you guys. Share if you have any other good alternative.

0 comments: