-1

select ProductId from Products I am getting the output as follows: ProductId

  1
  2
  3
  4

I want to get output as like follow:

ProductID

1,2,3,4
Biddut
  • 418
  • 1
  • 6
  • 17
  • 2
    Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the [How to ask](https://stackoverflow.com/help/how-to-ask) page for help clarifying this question. – John Joe Mar 14 '18 at 09:28
  • Which [DBMS](https://en.wikipedia.org/wiki/DBMS) are you using? "SQL" is just a query language, not the name of a specific database product. Please add a tag for the database product you are using `postgresql`, `oracle`, `sql-server`, `db2`, ... –  Mar 14 '18 at 09:49
  • Use `string_agg()` in Postgres –  Mar 14 '18 at 09:49
  • https://stackoverflow.com/questions/tagged/string-aggregation+sql –  Mar 14 '18 at 09:50

1 Answers1

1

Try This:

declare @aa varchar (200)
set @aa = ''

select @aa = 
case when @aa = ''
then cast(Productid as varchar)
else @aa + coalesce(',' + cast(Productid as varchar), '')
end
from Products

print @aa
mangesh
  • 355
  • 4
  • 13