Friday, September 19, 2008

Return Multi Row value in a single column with comma seprated

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

1 comment:

Anonymous said...

Great Tips Sir,
I always check out your poets and these are very very useful.
Thank u so much sir and keep posting these stuff for the people like me.