need a query/function to get the optput

Posted by Santosh4u under Sql Server on 2/25/2013 | Points: 10 | Views : 1062 | Status : [Member] | Replies : 4
Hi
any one please give me a sql query as per below output.

Table Nme :
ID EmpId Name
1 2 Ram
1 6 San
1 8 Tap

if i pass the value EmpIDs = '2,8' , as per the Empid Name should be displayed with comma separated in same row.

Output :
ID Name
1 Ram,Tap

Thanks
Santosh




Responses

Posted by: Murugavelmsc on: 2/25/2013 [Member] Starter | Points: 25

Up
0
Down
select id, group_concat(name) from tablename where empid in (2,8) group by id

Regards,
Murugavel S
murugavel.sadagopan@gmail.com
http://murugavelmsc.blogspot.in/

Santosh4u, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Santosh4u on: 2/25/2013 [Member] Bronze | Points: 25

Up
0
Down
Hi
group_concat' is not a recognized built-in function name error showing..

and even empids i have to pass as parameters to get the output..

Thanks
Santosh

Santosh4u, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Pandians on: 2/25/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
You can try with Corelated Sub query with FOR XML PATH('') option!

Cheers
www.SQLServerbuddy.blogspot.com
iLink Multitech Solutions

Santosh4u, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Oswaldlily on: 2/27/2013 [Member] Starter | Points: 25

Up
0
Down
/**CREATING FUNCTION **/
alter function funcvalue
(@emplid varchar(10)
,@empid2 varchar(10)
)
returns varchar(10) as
begin
declare @c varchar(10) ,@d varchar(10),@e varchar(10)
set @c=(Select name from tmp where empid= @emplid )
set @d=(Select name from tmp where empid=@empid2)
set @e=(Select @c + ','+@d)
return(@e)
end

/**** CALLING FUNCTION *****/
select dbo.funcvalue('2','8')


Santosh4u, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response