0

can i create a sql query that show me only one column but in these column are all the rows with semicolon. My example: Select Name from table1 -> Shows

[Name]
Peter
Doug
Mustafa
Emre

And now i want a query that show me that:

[Name]
Peter;Doug;Mustafa;Emre

Can u please help me!?

Greetz

2 Answers2

0
create table #user (username varchar(25))

insert into #user (username) values ('Peter')
insert into #user (username) values ('Doug')
insert into #user (username) values ('Mustafa')
insert into #user (username) values ('Emre')

declare @tmp varchar(250)
SET @tmp = ''
select @tmp = @tmp + username + ';' from #user

select SUBSTRING(@tmp, 0, LEN(@tmp))

modify as you need but it works

Noxious Reptile
  • 838
  • 1
  • 7
  • 24
  • Hi. its looks nice but i dont want to create a table! i found a mysql expression: Select GROUP_CONCAT(Name ';') Name from Table1 now I need the same for SQL – Artist_Styler_57 Feb 26 '16 at 10:08
0

thank you all for the help. Here is my result:

SELECT Name = STUFF((
SELECT ';' + Name
FROM Table1
FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 1, '')

Greetz