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
  • 9,480
  • 22
  • 70
  • 114

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
  • 33,101
  • 7
  • 58
  • 78
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
  • 91,357
  • 15
  • 148
  • 157