Click here to Skip to main content
15,908,843 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I have a query like this.

select pid,deptid from table1;

in which deptid is varchar and value like (1,3,4)

Now I have another table as department(deptid(int,PK), DeptName)

I want instead of get deptid from select I can get deptNames in csv.like

pid | deptname
1 | dept1,dept4,dept7
2 | dept4,dept9
What should be approach for this.

Thanks,
Posted
Comments
ArunRajendra 1-Aug-13 5:42am    
Is you data in table like this

pid deptid
1 1,3,4
2 4,5
rahul-4u 1-Aug-13 5:48am    
yes

First Create this Function:

SQL
Use DataBase_Name
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

Create Function [dbo].[fnSplitter] (@IDs Varchar(4000)  )  
Returns @Tbl_IDs Table (ID nvarchar(100) COLLATE SQL_Latin1_General_CP1_CS_AS  ) 
As  

Begin 
 -- Append comma
 Set @IDs =  @IDs + ',' 
 -- Indexes to keep the position of searching
 Declare @Pos1 Int
 Declare @Pos2 Int
 
 -- Start from first character 
 Set @Pos1=1
 Set @Pos2=1

 While @Pos1<len(@ids)>
 Begin
  Set @Pos1 = CharIndex(',',@IDs,@Pos1)
  Insert @Tbl_IDs Select  Substring(@IDs,@Pos2,@Pos1-@Pos2) 
  -- Go to next non comma character
  Set @Pos2=@Pos1+1
  -- Search from the next charcater
  Set @Pos1 = @Pos1+1
 End 
 Return
End

After Adding the Function Use this Query:
SQL
Select t.PId,Stuff((Select ','+DeptName 
                    From DeptTable 
                    Where DeptId In (Select ID From fnSplitter(t.DeptId)) 
                    For XML Path(''), TYPE)
                    .value('.','VARCHAR(max)'), 1, 1, '') [DeptName]
From Table t

Tested Code:
Create Table #Temp
	(
	PId int, 
	DeptId VarChar(40) COLLATE SQL_Latin1_General_CP1_CS_AS
	)

Create Table #Dept
	(
	DeptId VarChar(40) COLLATE SQL_Latin1_General_CP1_CS_AS, 
	DeptName VarChar(40) COLLATE SQL_Latin1_General_CP1_CS_AS
	)

Insert into #Temp 
Select 1,'1,2,3' Union all
Select 2,'4,5' 

Insert into #Dept 
Select '1','Dept1' Union All
Select '2','Dept2' Union All
Select '3','Dept3' Union All
Select '4','Dept4' Union All
Select '5','Dept5' 

Select t.PId,Stuff((Select ','+DeptName 
                    From #Dept 
                    Where DeptId In ( Select ID From fnSplitter(t.DeptId)) 
                    For XML Path(''), TYPE)
                    .value('.','VARCHAR(max)'), 1, 1, '') [DeptName] 
From #Temp t


Drop Table #Temp
Drop Table #Dept 

Output:
PId	DeptName
---     ---------
1	Dept1,Dept2,Dept3
2	Dept4,Dept5
 
Share this answer
 
v5
Create Table #Emp
	(
	PId int, 
	DeptId VarChar(40) 
	)
 
Create Table #Dept
	(
	DeptId VarChar(40) 
	DeptName VarChar(40) 
	)
 
Insert into #Emp 
Select 1,'1,2,3' Union all
Select 2,'4,5' 
 
Insert into #Dept 
Select '1','Dept1' Union All
Select '2','Dept2' Union All
Select '3','Dept3' Union All
Select '4','Dept4' Union All
Select '5','Dept5' 
 
SELECT pid, Depts = STUFF((SELECT ',' + d.DeptName 
    FROM #Dept AS d
    INNER JOIN #Emp AS ei
    ON ',' + ei.deptid + ',' LIKE '%,' + CONVERT(VARCHAR(12), d.deptid) + ',%'
    WHERE ei.pid = e.pid
    ORDER BY DeptName
    FOR XML PATH, TYPE).value('.[1]', 'nvarchar(max)'), 1, 1, '')
FROM #Emp AS e
ORDER BY pid;
 

Drop Table #Emp
Drop Table #Dept 
 
Share this answer
 
v2
Well you can use following approach.

* You create a function that will return comma separated department names associated with a given pid.

SQL
CREATE FUNCTION dbo.GetCommaSepDepartments(@pid int)
RETURNS varchar(1000)
WITH EXECUTE AS CALLER
AS
BEGIN
     DECLARE @temp varchar;
     SET @temp = (SELECT SUBSTRING(
                  (SELECT ',' + D.DeptName
                   FROM Table1 s
                   INNER JOIN Department D on s.deptid = D.Deptid
                   where s.pid = tpid
                   ORDER BY D.DeptName
                   FOR XML PATH('')
                  ),2,200000)
                 );

     RETURN(@temp);
END;


* After this you can use below query to get the final result



XML
<pre lang="sql">
select T1.pid, GetCommaSepDepartments(@tpid int)
from table1 T1;
</pre>
 
Share this answer
 
Comments
rahul-4u 1-Aug-13 6:23am    
I have csv ids to be passed in the function. it is not the int as @pid as you given the solution. can you provide me it with passing a string ids(as csv) and return string name(as csv).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900