Convert multiple rows into one with comma as separator
csv, rows, t-sql
Solution
This should work for you. Tested all the way back to SQL 2000.
create table #user (username varchar(25))
insert into #user (username) values ('Paul')
insert into #user (username) values ('John')
insert into #user (username) values ('Mary')
declare @tmp varchar(250)
SET @tmp = ''
select @tmp = @tmp + username + ', ' from #user
select SUBSTRING(@tmp, 0, LEN(@tmp))
Problem
If I issue `SELECT username FROM Users` I get this result: ``` username -------- Paul John Mary ``` but what I really need is one row with all the values separated by comma, like this: ``` Paul, John, Mary ``` How do I do this?