July 9, 2008 at 7:14 am
Hi
i have a table
tEmployee Column is Employeename
tEmployee
----------
Values
Employeename
--------------
scottt
Tiger
Huli
abrash
i want out put like
scott,tiger,huli,abrash in a single line
how to write select query for above output . please help.me.
Reagrds
swamy.
July 9, 2008 at 7:24 am
Perhaps something like:
declare @test-2 table (aName varchar(10))
insert into @test-2
select 'scott' union all
select 'Tiger' union all
select 'Huli' union all
select 'abrash'
--select * from @test-2
declare @accum varchar(max) set @accum = ''
select
@accum
= case when @accum <> ''
then @accum + ',' + aName
else aName
end
from @test-2
select @accum as [@accum]
/* -------- Sample Output: --------
@accum
--------------------------
scott,Tiger,Huli,abrash
*/
July 9, 2008 at 7:34 am
Thank u.
Actually i donot know the rows in the temployee table
Hi
i have a table
tEmployee Column is Employeename
tEmployee
----------
Values
Employeename
--------------
scottt
Tiger
Huli
abrash
...
....
....
.....
upto end of rows
actually
select 'scott' union all
select 'Tiger' union all
select 'Huli' union all
select 'abrash'
above values are fixed but i want from temployee table row values.
Ragards
Swamy.
July 9, 2008 at 7:35 am
I am seeing this done a lot using xml path. I have not done a lot of tests, but I hear it is more efficient.
declare @test-2 table (aName varchar(10))
insert into @test-2
select 'scott' union all
select 'Tiger' union all
select 'Huli' union all
select 'abrash'
--select * from @test-2
declare @accum varchar(max)
Select @accum= Stuff((
select ',' +
aName
from @test-2
for xml path('')
),1,1,'')
select @accum as [@accum]
July 9, 2008 at 7:42 am
Swamy m (7/9/2008)
Thank u.Actually i donot know the rows in the temployee table
It's just a way of showing that it actually works... the temporary table is just for demonstration purposes. You will need to change the code to use your real table name and not use the temporary table at all.
Please see the link in my signature line for more information... 😉
--Jeff Moden
Change is inevitable... Change for the better is not.
July 9, 2008 at 11:07 am
HI,
On what basis you want to concatinate? if ou have one Million records, you dont want to concatinate all the rows into one single column!!!.
So what is your actual requirment?
Thanks -- Vj
July 9, 2008 at 5:12 pm
Now, there's the 64 thousand dollar question! 🙂
--Jeff Moden
Change is inevitable... Change for the better is not.
July 10, 2008 at 12:05 am
How about COALESCE
Declare @vc as varchar(max)
Select @vc = COALESCE(@vc + ',','') + EmployeeName from tEmployee
Select @vc
Also, you can use where clause to filter the Select statament
July 10, 2008 at 12:54 am
Thank u So much . it is working.:cool:
Declare @vc as varchar(max)
Select @vc = COALESCE(@vc + ',','') + EmployeeName from tEmployee
Select @vc
Regards
swamy.
Viewing 9 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply