ALTER FUNCTION dbo.FN_GetTicketElapsedTime
(
@o_StartDate DATETIME
, @o_EndDate DATETIME
)
RETURNS REAL
AS
BEGIN
DECLARE @x_StartDate AS CHAR(08) -- yyyymmdd
, @x_StartTime AS CHAR(08) -- hh:mm:ss (military)
, @x_EndDate AS CHAR(08)
, @x_EndTime AS CHAR(08)
, @x_ElapsedSeconds AS REAL
, @x_condteOffStartTime AS CHAR(08)
, @x_condblOffEndTime AS CHAR(08)
, @x_condblDayEndTime AS CHAR(08)
, @x_IntervalDays AS INTEGER
--Provide default for error cases
SELECT @x_ElapsedSeconds = 0
If (@o_StartDate < @o_EndDate)
BEGIN
--Determine actual value for all others
-- Set the ticket start and end dates and times
SELECT @x_StartDate = CONVERT(CHAR(08), @o_StartDate, 112)
, @x_StartTime = CONVERT(CHAR(08), @o_StartDate, 108)
, @x_EndDate = CONVERT(CHAR(08), @o_EndDate, 112)
, @x_EndTime = CONVERT(CHAR(08), @o_EndDate, 108)
-- Excluded from the time calculations are 3am to 10am
, @x_condteOffStartTime = '03:00:00'
, @x_condblOffEndTime = '10:00:00'
, @x_condblDayEndTime = '23:59:59'
-- 2.0 Ticket opened and closed on the same day.
If (@x_StartDate = @x_EndDate)
-- 2.1 Early-morning opening before start of cutoff
If (@x_StartTime < @x_condteOffStartTime)
BEGIN
If (@x_EndTime < @x_condteOffStartTime) --early close too
SELECT @x_ElapsedSeconds = DATEDIFF(second,@x_StartTime,@x_EndTime)
Else --closes later - into cutoff time
BEGIN
SELECT @x_ElapsedSeconds = DATEDIFF(second,@x_StartTime,@x_condteOffStartTime)
If (@x_EndTime > @x_condblOffEndTime) --closes after end of cutoff
SELECT @x_ElapsedSeconds = @x_ElapsedSeconds + (DATEDIFF(second,@x_condblOffEndTime, @x_EndTime))
-- ELSE there is nothing to calculate
END
END
-- 2.2 Opening within cutoff period
Else
BEGIN
If ((@x_StartTime >= @x_condteOffStartTime) And (@x_StartTime < @x_condblOffEndTime))
BEGIN
If (@x_EndTime >= @x_condblOffEndTime) --closes later - after cutoff end
SELECT @x_ElapsedSeconds = @x_ElapsedSeconds + (DATEDIFF(second,@x_condblOffEndTime, @x_EndTime))
-- ELSE close in cutoff too ,so nothing to calculate
END
-- 2.3 Opening after cutoff (closes same day)
Else
SELECT @x_ElapsedSeconds = DATEDIFF(second,@x_StartTime, @x_EndTime)
END
--3. Ticket opened and closed on different days
Else
BEGIN
-- 3.1 First day
-- 3.1.1 Early-morning opening before start of cutoff
If (@x_StartTime < @x_condteOffStartTime)
BEGIN
SELECT @x_ElapsedSeconds = DATEDIFF(second,@x_StartTime,@x_condteOffStartTime) --initial period
SELECT @x_ElapsedSeconds = @x_ElapsedSeconds + (DATEDIFF(second,@x_condblOffEndTime, @x_condblDayEndTime)) --rest of day 1
END
-- 3.1.2 Opening within cutoff period
Else
BEGIN
If ((@x_StartTime >= @x_condteOffStartTime) And (@x_StartTime < @x_condblOffEndTime))
SELECT @x_ElapsedSeconds = DATEDIFF(second,@x_condblOffEndTime, @x_condblDayEndTime) --rest of day 1
-- 3.1.3 Opening after cutoff (closes some future day)
Else
SELECT @x_ElapsedSeconds = DATEDIFF(second,@x_StartTime,@x_condblDayEndTime)
END
-- 3.2 Intervening full days (in example 00:00-03:00 and 10:00-24:00 added in)
SELECT @x_IntervalDays = DATEDIFF(day, @o_StartDate, @o_EndDate)
If (@x_IntervalDays > 1) -- In the calculation, exclude the last day, will be calculated later in 3.3
SELECT @x_ElapsedSeconds = @x_ElapsedSeconds + ((@x_IntervalDays - 1) * (DATEDIFF(second,'00:00:00', @x_condteOffStartTime) + DATEDIFF(second,@x_condblOffEndTime,@x_condblDayEndTime) ))
-- 3.3 Closing day
-- 3.3.1 Early-morning close before start of cutoff
If (@x_EndTime < @x_condteOffStartTime)
SELECT @x_ElapsedSeconds = @x_ElapsedSeconds + DATEDIFF(second,'00:00:00',@x_EndTime)
-- 3.3.2 Close within cutoff period
Else
BEGIN
If ((@x_EndTime >= @x_condteOffStartTime) And (@x_EndTime < @x_condblOffEndTime))
SELECT @x_ElapsedSeconds = @x_ElapsedSeconds + DATEDIFF(second,'00:00:00',@x_condteOffStartTime)
-- 3.3.3 Opening after cutoff
Else
SELECT @x_ElapsedSeconds = @x_ElapsedSeconds + (DATEDIFF(second,'00:00:00',@x_condteOffStartTime) + (DATEDIFF(second,@x_condblOffEndTime, @x_EndTime)))
END
END
END
--4. Convert value to elapsed hours
SELECT @x_ElapsedSeconds = ((@x_ElapsedSeconds /60)/60)
RETURN @x_ElapsedSeconds
END