Quick Search

Return Multi Row value in a single column with comma seprated

February 27th, 2010 by admin
Leave a reply »

 

Return Multi Row value in a single column with comma seprated using sql
I want Return output like 1,2,3,4,5 using sql.
There is one table in which has column
id name
1 Dilip
2 Dinesh
3 Varun
4 Dhanpat
5 Renuka

solution:
declare @a varchar(500)
set @a = ‘ ‘
select @a = @a + cast(id as varchar(20)) + ‘,’ from tablename
if len(@a)>1
set @a= left(@a,len(@a)-1)
select case @a when ” then ’0′ else @a end as id

Use case , becaue may be there is no any value in this table so it will retun o

Posted in Database

You can follow any responses to this entry through the RSS 2.0 Feed. You can leave a response , or trackback from your own site.

Leave a Reply