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
SET @nov = '1/11/' + CAST(@x as VARCHAR )
SET @feb = '1/2/' + CAST((@x + 1) as VARCHAR )
SET @may = '15/5/' + CAST((@x + 1) as VARCHAR )
If @STPRG_START <= @nov And @STPRG_END >= @may SET @tap = @tap + 3
Else If @STPRG_START <= @nov And @STPRG_END >= @feb SET @tap = @tap + 2
Else If @STPRG_START <= @nov And @STPRG_END >= @nov SET @tap = @tap + 1
Else If @STPRG_START <= @feb And @STPRG_END >= @may SET @tap = @tap + 2
Else If @STPRG_START <= @feb And @STPRG_END >= @feb SET @tap = @tap + 1
Else If @STPRG_START <= @may And @STPRG_END >= @may SET @tap = @tap + 1
Else
SET @tap = @tap
End If
SET @x = @x + 1
END
If @tap = 0 SET @tap = 1
Return @tap