CREATE FUNCTION fn_Split(@string nvarchar(2000), @separator nvarchar(2000), @index int)
RETURNS nvarchar(2000)
AS
BEGIN
DECLARE @current_position int
DECLARE @counter int
SET @current_position = 0
SET @counter = 1
WHILE CHARINDEX(@separator, @string, @current_position) > 0
BEGIN
IF @counter = @index
RETURN SUBSTRING(@string, @current_position, CHARINDEX(@separator, @string, @current_position) - @current_position)
SET @current_position = CHARINDEX(@separator, @string, @current_position) + 1
SET @counter = @counter + 1
END
RETURN SUBSTRING(@string, @current_position, LEN(@string))
END