0

I am using SQL Server 2005 and would like to find an easier way to concat multiple rows into 1 string.

PK Column1, Column2
--  -------  -------
PK1 apple    orange
PK1 pear     banana
PK1 honey
PK2 apple2    orange2
PK2 pear2     banana2
PK2 honey2

Results :

PK1, apple orange pear banana honey
PK2, apple2 orange2 pear2 banana2 honey2

It is very easy to use COALESCE but it is not available in SQL Server 2005. I tried XML Path but it appends additional character to the end.

Could you please provide me some suggestion ? Thanks.

3 Answers 3

1

A far easier solution is to use XML PATH

SELECT @cols = STUFF((SELECT DISTINCT ',' + QUOTENAME(a.Column1)
FROM dbo.mytbl AS a WHERE a.ColumnX = somecondition
FOR XML PATH ( '' ) , TYPE).value('.', 'NVARCHAR(MAX)'), 1, 1, '')

SELECT @cols

Of course the WHERE clause is optional in your case. And without spoon feeding you, apply the same to your other column and concatenate them. Voila!

Sign up to request clarification or add additional context in comments.

Comments

0

When your data is some blank spaces ' ' in result of for xml path you will see a   at the end like '  ':

select '    ' for xml path ('');

In your case I can use this query:

select t.PK, 
    ltrim(rtrim(replace(
    (select ' ' + isnull(ti.Column1, '') + ' ' + isnull(ti.Column2, '')
     from yourTable ti 
     where ti.PK = t.PK
     for xml path (''))
     , ' ', ''))) fruits
from yourTable t
group by t.PK;

Comments

0

This will trap null or empty values

Declare @YourTable table (PK int,Column1 varchar(25), Column2 varchar(25))
Insert Into @YourTable values
(1,'apple','orange'),
(1,'pear','banana'),
(1,'honey', null),
(2,'apple2','orange2'),
(2,'pear2','banana2'),
(2,'honey2', null)

Select PK
      ,DelimString = Stuff((Select case when Column1 is null or Column1='' then '' else ' ' + replace(Column1,char(13),'') end
                                  +case when Column2 is null or Column2='' then '' else ' ' + replace(Column2,char(13),'') end  
                             From  @YourTable
                             Where PK=A.PK
                             For XML  Path('')), 1, 1, '') 
 From @YourTable A
 Group By PK

Returns

PK  DelimString
1   apple orange pear banana honey 
2   apple2 orange2 pear2 banana2 honey2 

15 Comments

Thanks John. For some reason, using XML Path, it appended &#x0D to the end of the string. Will see how I can remove it.
@user3015739 That is a line feed
I am trying to figure out where to put the TEXT command per what described here : stackoverflow.com/questions/16547175/…
Yeah, I can use a REPLACE function to remove it. Now, I added some new tweak where I need another column in front.
Cappeletti, you are excellent. I will tweak it a bit to differentiate my PK column. Thanks.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.