Display Database column as comma separated value in SQL Server

Last Reply 4 months ago By dharmendr

Posted 4 months ago

Hi,

I have one table

Id   Email

1     aaa@gmail.com

2     bb@gmail.com

i have one button after clicking of that button i need to fetch all emails from database and display it with comma separated values

Ex o/p:aaa@gmail.com,bb@gmail.com

You are viewing reply posted by: dharmendr 4 months ago.
Posted 4 months ago

Hi mahesh213,

Use query like below and implement in your code to dispaly the record.

DECLARE @CSV VARCHAR(MAX) 
SELECT @CSV = COALESCE(@CSV + ', ', '') + FirstName from Employees
SELECT @CSV AS Result