Abhi.Net
Abhi.Net

Reputation: 772

Convert Row values to concatenated string

I am trying to convert data from rows to a multiple concatenated strings

HH  3(6.27)
HH  4(4.48)
LH  5(0)
HH  6(2.27)
HH  7(0)
LL  31(0)
LM  32(0)

This is the result of one of the sub queries, How do I convert this to

HH 3(6.27), 4(4.48), 6(2.27), 7(0)
LH  5(0)
LL  31(0)
LM  32(0)

Upvotes: 0

Views: 35

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1271151

You are looking for aggregate string concatenation. Here is how you do this in SQL Server:

with cte(col1, col2) as (
      < your subquery here >
     )
select distinct col1,
       stuff((select ', ' + col2
              from cte cte2
              where ct2.col1 = cte.col1
              for xml path ('')
             ), 1, 2, '') as col2s
from cte;

Upvotes: 2

Related Questions