0

I need to get the value from one query as a single row, separated by one separator, chosen but me, but it's a little tricky.

So, I have:

select employer from employers

I will return:

(line1)    Anne
(line2)    Sue
(line3)    Villy
(line4)    John
(nth line) Marry

I need to have the result as this:

'Anne','Sue','Villy','John','Marry'

Or at least:

 Anne^Sue^Villy^John^Marry

Any hints? Thanks,

bobs
  • 21,844
  • 12
  • 67
  • 78
BogdanM
  • 957
  • 4
  • 15
  • 32

2 Answers2

2
declare @s varchar(max) = ''

select @s = case 
    when @s <> '' then  
        @s + ',''' + employer + ''''
    else 
        @s + '''' + employer + '''' end 
from employers

select @s
Kevin Collins
  • 1,453
  • 1
  • 10
  • 16
2
SELECT STUFF((SELECT DISTINCT  ', ' + Employer AS [text()]
FROM Employers
FOR XML PATH ('')),1,1,'')
TEEKAY
  • 1,156
  • 1
  • 10
  • 25