0

If I issue SELECT ID FROM TestAhmet I get this result:

1
3
5
2
4

but what I really need is one row with all the values separated by comma, like this:

1,2,3,4,5

How do I do this?

ps: I cant do this : Convert multiple rows into one with comma as separator

Community
  • 1
  • 1

2 Answers2

1

This should work:

select stuff((select ',' + cast(id as varchar(8000))
              from TestAhmet
              for xml path ('')
             ), 1, 1, '') as users

This is a variation on the string aggregation logic often used in SQL Server. But, without a group by, you probably won't find many examples on the web.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
1

If id is numeric column then do like this

select stuff((select ',' +Convert(varchar(50),id)
              from TestAhmet
              for xml path ('')
             ), 1, 1, '') as users
Mukesh Kalgude
  • 4,814
  • 2
  • 17
  • 32