IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[chaifenlie]') AND type = 'FN')
DROP FUNCTION [dbo].[chaifenlie]
GO

create function dbo.chaifenlie 
( 
 @Expression varchar(8000) 
,@Delimiter varchar(100) 
,@ int 
) 
returns varchar(8000) 
as 
begin 


declare @p int 
set @p = CharIndex(@Delimiter,@Expression) 
if @p > 0
begin
   set @p = @p + len(@Delimiter) - 1
end
declare @i int 
set @i = 1 
while @i < @ 
begin  
   set @i = @i + 1
   set @Expression = substring (@Expression, @p + 1,len(@Expression) - @p ) 
   set @p = CharIndex(@Delimiter,@Expression)
   if @p > 0
   begin
      set @p = @p + len(@Delimiter) - 1
   end
   else
   begin
      break
   end
end 

declare @s varchar(1000) 
if @p = 0 and @i = @
begin 
   set @s = @Expression
end 
else if @i = @ 
begin 
   set @s = substring(@Expression, 1,@p - len(@Delimiter)) 
end 
return @s
end