Saturday, 18 May 2013

Sql server:Column data in comma seprated string

Column data in comma seprated string

Sql server:Column data in comma seprated string

A sql sever provide you a inbuild function COALESCE Function
to select a Column data of a table,to a comma seprated string.
By using bellow query we can select column data in comma separated string.

Example::

    create table #temp(id int ,names varchar(50) )

    insert into #temp(id,names)values (1,'SandipG')
    insert into #temp(id,names)values (1,'Aditya')
    insert into #temp(id,names)values (1,'Ajit')
    insert into #temp(id,names)values (1,'Chandu')

A query for select column data in comma separated string::

DECLARE @CommaSeperatedValues VARCHAR (MAX)
SELECT @CommaSeperatedValues = COALESCE (@CommaSeperatedValues+',' , '') + names
FROM #temp
print @CommaSeperatedValues

1 comment:

8451831077