Asp.net blogs

Friday, 7 March 2014

Query to generate comma-separated list from one individual column data from database table in sql server using COALESCE()

Table Name > tbmap
                    Fields >  Place
                                    Data > Amritsar
                                           Bhutan
                                           Kerala
                                          

Query to generate comma-separated list from one individual column data
from database table in sql server using COALESCE()


Query >

 DECLARE @List VARCHAR(8000)

SELECT @List = COALESCE(@List + ',', '') + CAST(Place AS VARCHAR)
FROM   tbmap
where Place='PLACENAME'
SELECT @List as Place

Output :
Amritsar,Bhutan,Kerala