0

I have the following:

BEGIN TRANSACTION

CREATE TABLE #temp
(
    testing VARCHAR(MAX)
)
INSERT INTO #temp
  ( testing )
VALUES ('Boomerang')
INSERT INTO #temp
  ( testing )
VALUES ('Bowling')
INSERT INTO #temp
  ( testing )
VALUES ('Boxing')
INSERT INTO #temp
  ( testing )
VALUES ('Bull Fighting')


SELECT *
FROM #temp

ROLLBACK TRANSACTION

And I'm trying to display it as a single column result like the following:

Boomerang|Bowling|Boxing|Bull Fighting

However I've no idea how to do this. I've looked into Pivot but it doesn't seem to address my issue.

Michael A
  • 8,839
  • 20
  • 69
  • 110

2 Answers2

3

This is another way of doing it using string concatenation with Isnull() [or coalesce()] function.

Fiddle demo:

declare @str varchar(max)

Select @str = isnull(@str + '|', '') + testing
From temp
Order by testing

Select @str
--Results
Boomerang|Bowling|Boxing|Bull Fighting
Kaf
  • 32,033
  • 7
  • 53
  • 77
2
SELECT STUFF(
        (
          SELECT '|' + testing
            FROM #temp
             FOR XML PATH(''), TYPE
        ).value('.', 'NVARCHAR(MAX)'), 1, 1, '') list

Output:

| LIST                                   |
|----------------------------------------|
| Boomerang|Bowling|Boxing|Bull Fighting |

Here is SQLFiddle demo

peterm
  • 88,818
  • 14
  • 143
  • 153