CREATE FUNCTION TOTAL_TAP(@STPRG_START DateTime, @STPRG_END DateTime)
RETURNS integer
AS
BEGIN
Declare @nov As DateTime
Declare @feb As DateTime
Declare @may As DateTime
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
SET @x = @x + 1
END
If @tap = 0 SET @tap = 1
Return @tap
END