Steven-Harris
4/15/2016 - 2:53 PM

A list of strings to join by a column in another table Split list of strings into a column of a table

A list of strings to join by a column in another table Split list of strings into a column of a table

/****** Object:  UserDefinedFunction [dbo].[SplitEmpJDEKeys]    Script Date: 4/15/2016 9:52:15 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER FUNCTION [dbo].[SplitKeys](@String varchar(max), @Delimiter char(1))       
returns @temptable TABLE (Key varchar(max))       
as       
begin       
    declare @idx int       
    declare @slice varchar(max)       

    select @idx = 1       
        if len(@String)<1 or @String is null  return       

    while @idx!= 0       
    begin       
        set @idx = charindex(@Delimiter,@String)       
        if @idx!=0       
            set @slice = left(@String,@idx - 1)       
        else       
            set @slice = @String       

        if(len(@slice)>0)  
            insert into @temptable(Key) values(@slice)       

        set @String = right(@String,len(@String) - @idx)       
        if len(@String) = 0 break       
    end   
return       
end 

ALTER PROCEDURE [dbo].[stp_GetEmployeeDetailsByEmployeeList] 
		@Emplist as varchar(max),
	@AreaID as int
AS
select * into #tmpKeys from dbo.splitKeys(@Emplist,',')

SELECT  DISTINCT
	E.Key
	.
	.
	,ROW_NUMBER() OVER (PARTITION BY e.Key ORDER BY RAS.SubmittedDate DESC) AS rn 
INTO #TMP
FROM 
.
.
.
LEFT OUTER JOIN 
	#tmpKeys as k on k.Key = e.Key
LEFT OUTER JOIN
	dbo.Area as a on RA.AreaID = a.AreaID
WHERE
	Ra.inactive = 0
	and a.areaID = @AreaID
	and E.Key is not null
	and s.status_Description is not null
ORDER BY
	A.AreaName

Select * from #TMP where rn =1
Drop table #tmp
Drop table #tmpKeys