CREATE FUNCTION [dbo].[RepText]
(	
	@StartDate Varchar(10),
	@EndDate Varchar(10)
)
RETURNS @RepText TABLE 
(
  Note Varchar(28)
)
AS
Begin
    Declare @Note Varchar(28)
    Declare @Year Char(4)
    Declare @Month Char(2)
    Declare @Day Char(2)
    Declare @UserId Smallint
    Declare @Tarikh Smalldatetime
    Declare @Enter1 nvarchar(5)
    Declare @Enter2 nvarchar(5)
    Declare @Enter3 nvarchar(5)
    Declare @Enter4 nvarchar(5)
    Declare @Enter5 nvarchar(5)
    Declare @Exit1  nvarchar(5)
    Declare @Exit2  nvarchar(5)
    Declare @Exit3  nvarchar(5)
    Declare @Exit4  nvarchar(5)
    Declare @Exit5  nvarchar(5)
    Declare @Guy    varchar(8)
    DEclare @CodeSet varchar(5)
    
    Set @CodeSet = '00001'
    DECLARE Select_Cursor CURSOR FOR 
	SELECT Userid, Convert(Varchar(10), Tarikh_Sys, 111) Tarikh, ARRIV_1, DEPARCH_1, ARRIV_2, DEPARCH_2,ARRIV_3, DEPARCH_3,ARRIV_4, DEPARCH_4,ARRIV_5, DEPARCH_5 FROM Karkerd
	Where Dbo.MiladiToShamsi(Tarikh_Sys) >= @StartDate
	And Dbo.MiladiToShamsi(Tarikh_Sys) <= @EndDate
	OPEN Select_Cursor 
	FETCH NEXT FROM Select_Cursor  INTO @userId, @Tarikh, @enter1, @Exit1, @enter2, @Exit2, @enter3, @Exit3, @enter4, @Exit4, @enter5, @Exit5
	WHILE @@FETCH_STATUS = 0 
	BEGIN 
 	  Select @Year = substring(dbo.MiladiToShamsi(@Tarikh),1,4)
 	  Select @Month = substring(dbo.MiladiToShamsi(@Tarikh),6,2)
 	  Select @Day = substring(dbo.MiladiToShamsi(@Tarikh),9,2)
 	  Select @Guy = REPLICATE('0', 8- LEN(@userId))+ Cast(@UserId as Varchar(10))
 	  if (@Enter1 <> '') 
 	  begin
 	    Insert Into @RepText (Note)
 	    Values(Ltrim(Rtrim(@Guy + @Year + '/'+ @Month + '/' +@Day + @Enter1 + @CodeSet))) 
 	  end
 	  if (@Exit1 <> '') 
 	  begin
 	    Insert Into @RepText (Note)
 	    Values(LTrim(RTrim(@Guy + @Year + '/'+ @Month + '/' +@Day + @Exit1 + @CodeSet))) 
 	  end
 	  
 	  if (@Enter2 <> '') 
 	  begin
 	    Insert Into @RepText (Note)
 	    Values(LTrim(RTrim(@Guy + @Year + '/'+ @Month + '/' +@Day + @Enter2 + @CodeSet))) 
 	  end
 	  if (@Exit2 <> '') 
 	  begin
 	    Insert Into @RepText (Note)
 	    Values(LTrim(RTrim(@Guy + @Year + '/'+ @Month + '/' +@Day + @Exit2 + @CodeSet))) 
 	  end
 	  
 	  if (@Enter3 <> '') 
 	  begin
 	    Insert Into @RepText (Note)
 	    Values(LTrim(RTrim(@Guy + @Year + '/'+ @Month + '/' +@Day + @Enter3+ @CodeSet))) 
 	  end
 	  if (@Exit3 <> '') 
 	  begin
 	    Insert Into @RepText (Note)
 	    Values(LTrim(RTrim(@Guy + @Year + '/'+ @Month + '/' +@Day + @Exit3 + @CodeSet))) 
 	  end
 	  
 	  if (@Enter4 <> '') 
 	  begin
 	    Insert Into @RepText (Note)
 	    Values(LTrim(RTrim(@Guy + @Year + '/'+ @Month + '/' +@Day + @Enter4 + @CodeSet))) 
 	  end
 	  if (@Exit4 is not null And @Exit4 <> '') 
 	  begin
 	    Insert Into @RepText (Note)
 	    Values(LTrim(RTrim(@Guy + @Year + '/'+ @Month + '/' +@Day + @Exit4 + @CodeSet))) 
 	  end

 	  if (@Enter5 <> '') 
 	  begin
 	    Insert Into @RepText (Note)
 	    Values(LTrim(RTrim(@Guy + @Year + '/'+ @Month + '/' +@Day + @Enter5 + @CodeSet))) 
 	  end
 	  if (@Exit5 <> '') 
 	  begin
 	    Insert Into @RepText (Note)
 	    Values(LTrim(RTrim(@Guy + @Year + '/'+ @Month + '/' +@Day + @Exit5 + @CodeSet))) 
 	  end

   	  FETCH NEXT FROM Select_Cursor  INTO @userId, @Tarikh, @enter1, @Exit1, @enter2, @Exit2, @enter3, @Exit3, @enter4, @Exit4, @enter5, @Exit5
	END 
	Close Select_Cursor 
	Deallocate Select_Cursor 
    RETURN
End
