CREATE FUNCTION TOTAL_TAP(@STPRG_START Date, @STPRG_END Date) RETURNS integer AS BEGIN Declare @nov As Date Declare @feb As Date Declare @may As Date Declare @tap As Integer Declare @x As Integer SET @tap = 0 SET @x = 1995 WHILE (@x < 2009) BEGIN @nov = Date('1/11/' + CAST(@x as VARCHAR )) @feb = Date('1/2/' + CAST((@x + 1) as VARCHAR )) @may = Date('15/5/' + CAST((@x + 1) as VARCHAR )) If @STPRG_START <= @nov And @STPRG_END >= @may Then @tap = @tap + 3 ElseIf @STPRG_START <= @nov And @STPRG_END >= @feb Then @tap = @tap + 2 ElseIf @STPRG_START <= @nov And @STPRG_END >= @nov Then @tap = @tap + 1 ElseIf @STPRG_START <= @feb And @STPRG_END >= @may Then @tap = @tap + 2 ElseIf @STPRG_START <= @feb And @STPRG_END >= @feb Then @tap = @tap + 1 ElseIf @STPRG_START <= @may And @STPRG_END >= @may Then @tap = @tap + 1 Else @tap = @tap End If @x = @x + 1 END If @tap = 0 Then @tap = 1 Return @tap END