/*************************************************************************
Function Name: WorkTimeOpen
Type: Scaler
Designer: Evadman
Last Updated: Tue May 10 16:17:12 CDT 2005 @928 /Internet Time/
Updated by: Evadman
Information: This function returns the number of work time seconds between 2 times.
Format: Integer
Length: 4
************************************************************************
Version: 1.00
Date: Tue May 10 16:17:10 CDT 2005 @928 /Internet Time/
***************************************************************************/
CREATE Function WorkTimeOpen
(
@BegDate DateTime,
@EndDate datetime,
@WeekdayHourOpen int = NULL,
@WeekdayHourClosed int = NULL,
@SatHourOpen int = NULL,
@SatHourClosed int = NULL,
@SunHourOpen int = NULL,
@SunHourClosed int = NULL
)
Returns int
AS
Begin
DECLARE @TimeStart DateTime
DECLARE @TimeEnd DateTime
DECLARE @TempDate DateTime
DECLARE @TmpSeconds int
SET @TmpSeconds = 0
IF @WeekdayHourOpen = NULL
SET @WeekdayHourOpen = 0
IF @WeekdayHourClosed = NULL
SET @WeekdayHourClosed = 0
IF @SatHourOpen = NULL
SET @SatHourOpen = 0
IF @SatHourClosed = NULL
SET @SatHourClosed = 0
IF @SunHourOpen = NULL
SET @SunHourOpen = 0
IF @SunHourClosed = NULL
SET @SunHourClosed = 0
/* return backwards days if beg and end date are backwards, basicly always return a pos number */
If @EndDate < @BegDate
BEGIN
SET @TimeEnd = @BegDate
SET @TimeStart = @EndDate
END
Else
BEGIN
SET @TimeEnd = @EndDate
SET @TimeStart = @BegDate
END
/*round the times to the ending/beginning time (start times) */
If DATEPART(dw, @TimeStart) = 1
BEGIN
If DATEPART(hh, @TimeStart) < @SunHourOpen
BEGIN
SET @TimeStart = CAST(convert(varchar,@TimeStart,101) AS DateTime) -- start time before open
SET @TimeStart = DateAdd(hh, @SunHourOpen, @TimeStart)
END
If DATEPART(hh, @TimeStart) >= @SunHourClosed
BEGIN
SET @TimeStart = CAST(convert(varchar,@TimeStart,101) AS DATETIME) --start time before open
SET @TimeStart = DateAdd(hh, @SunHourClosed, @TimeStart)
END
END
If DATEPART(dw, @TimeStart) = 7
BEGIN
If DATEPART(hh, @TimeStart) < @SatHourOpen
BEGIN
SET @TimeStart = CAST(convert(varchar,@TimeStart,101) AS DATETIME) -- start time before open
SET @TimeStart = DateAdd(hh, @SatHourOpen, @TimeStart)
END
If DATEPART(hh, @TimeStart) >= @SatHourClosed
BEGIN
SET @TimeStart = CAST(convert(varchar,@TimeStart,101) AS DATETIME) -- start time before open
SET @TimeStart = DateAdd(hh, @SatHourClosed, @TimeStart)
End
END
IF DATEPART(dw, @TimeStart) > 1 AND DATEPART(dw, @TimeStart) < 7
BEGIN
If DATEPART(hh, @TimeStart) < @WeekdayHourOpen
BEGIN
SET @TimeStart = CAST(convert(varchar,@TimeStart,101) AS DATETIME) --start time before open
SET @TimeStart = DateAdd(hh, @WeekdayHourOpen, @TimeStart)
END
If DATEPART(hh, @TimeStart) >= @WeekdayHourClosed
BEGIN
SET @TimeStart = CAST(convert(varchar,@TimeStart,101) AS DATETIME) -- start time before open
SET @TimeStart = DateAdd(hh, @WeekdayHourClosed, @TimeStart)
END
END
/* round the times to the ending/beginning time (end times) */
If DATEPART(dw, @TimeEnd) = 1 -- sunday
BEGIN
If DATEPART(hh, @TimeEnd) >= @SunHourClosed
BEGIN
SET @TimeEnd = CAST(convert(varchar,@TimeEnd,101) AS DATETIME) -- start time before open
SET @TimeEnd = DateAdd(hh, @SunHourClosed, @TimeEnd)
END
If DATEPART(hh, @TimeEnd) < @SunHourOpen
BEGIN
SET @TimeEnd = CAST(convert(varchar,@TimeEnd,101) AS DATETIME) -- start time before open
SET @TimeEnd = DateAdd(hh, @SunHourOpen, @TimeEnd)
END
END
If DATEPART(dw, @TimeEnd) = 7 -- saturday
BEGIN
If DATEPART(hh, @TimeEnd) >= @SatHourClosed
BEGIN
SET @TimeEnd = CAST(convert(varchar,@TimeEnd,101) AS DATETIME) -- start time before open
SET @TimeEnd = DateAdd(hh, @SatHourClosed, @TimeEnd)
END
If DATEPART(hh, @TimeEnd) < @SatHourOpen
BEGIN
SET @TimeEnd = CAST(convert(varchar,@TimeEnd,101) AS DATETIME) -- start time before open
SET @TimeEnd = DateAdd(hh, @SatHourOpen, @TimeEnd)
END
END
IF DATEPART(dw, @TimeStart) > 1 AND DATEPART(dw, @TimeStart) < 7
BEGIN
If DATEPART(hh, @TimeEnd) >= @WeekdayHourClosed
BEGIN
SET @TimeEnd = CAST(convert(varchar,@TimeEnd,101) AS DATETIME) -- start time before open
SET @TimeEnd = DateAdd(hh, @WeekdayHourClosed, @TimeEnd)
END
If DATEPART(hh, @TimeEnd) < @WeekdayHourOpen
BEGIN
SET @TimeEnd = CAST(convert(varchar,@TimeEnd,101) AS DATETIME) -- start time before open
SET @TimeEnd = DateAdd(hh, @WeekdayHourOpen, @TimeEnd)
END
END
/* if the datevalues are on different days, add the number of workseconds for each day */
While DateDiff(dd, @TimeStart, @TimeEnd) > 0
BEGIN
/* add the number of work seconds in the current day of week to the total. */
If DATEPART(dw, @TimeStart) = 1
BEGIN
SET @TempDate = cast(convert(varchar,@TimeStart,101) as DATETIME)
SET @TempDate = DateAdd(hh, @SunHourClosed, @TempDate)
SET @TmpSeconds = @TmpSeconds + DateDiff(ss, @TimeStart, @TempDate)
END
If DATEPART(dw, @TimeStart) = 7
BEGIN
SET @TempDate = cast(convert(varchar,@TimeStart,101) as DATETIME)
SET @TempDate = DateAdd(hh, @SatHourClosed, @TempDate)
SET @TmpSeconds = @TmpSeconds + DateDiff(ss, @TimeStart, @TempDate)
END
If DATEPART(dw, @TimeStart) > 1 AND DATEPART(dw, @TimeStart) < 7
BEGIN
SET @TempDate = cast(convert(varchar,@TimeStart,101) as DATETIME)
SET @TempDate = DateAdd(hh, @WeekdayHourClosed, @TempDate)
SET @TmpSeconds = @TmpSeconds + DateDiff(ss, @TimeStart, @TempDate)
END
/* add a day */
SET @TimeStart = DateAdd(D, 1, @TimeStart)
/* change the start time of the current day to the beginning of the workday. */
If DATEPART(dw, @TimeStart) = 1
BEGIN
SET @TimeStart = CAST(convert(varchar,@TimeStart,101) AS DATETIME) --start time at open
SET @TimeStart = DateAdd(hh, @SunHourOpen, @TimeStart)
END
If DATEPART(dw, @TimeStart) = 7
BEGIN
SET @TimeStart = CAST(convert(varchar,@TimeStart,101) AS DATETIME) --start time at open
SET @TimeStart = DateAdd(hh, @SatHourOpen, @TimeStart)
END
If DATEPART(dw, @TimeStart) > 1 AND DATEPART(dw, @TimeStart) < 7
BEGIN
SET @TimeStart = CAST(convert(varchar,@TimeStart,101) AS DATETIME) --start time at open
SET @TimeStart = DateAdd(hh, @WeekdayHourOpen, @TimeStart)
END
END
/* if the date value of the 2 dates is the same, use datediff to figure out the difference */
If DateDiff(dd, @TimeStart, @TimeEnd) = 0
SET @TmpSeconds = @TmpSeconds + DateDiff(S, @TimeStart, @TimeEnd)
RETURN @TmpSeconds
END