Zebra - Datoberegning uden weekender samt business hours
Zebra,som lovet her er et åbent spørgsmål - 200 point
Jeg har fået foreslået nedenstående til at eksludere weekender i en beregning.
Kan man evt. udvide så man kan angive at man også ønsker at eksludere timer udenfor åbningsttid?
Fx.
startTime = 0800
endTime = 1800
DECLARE
@StartDate DATETIME
,@EndDate DATETIME
,@CorrectedStartDate DATETIME
,@CorrectedEndDate DATETIME
SET @StartDate = '2008-09-06 17:58'
SET @EndDate = '2008-09-13 21:59'
SET @CorrectedStartDate = (
CASE
WHEN (DATEPART(dw, @StartDate) + @@DATEFIRST) % 7 = 0 then CAST(FLOOR(CAST(DATEADD(DD,2,@StartDate) AS FLOAT )) AS DATETIME) -- 0: Saturday -> forward 2 days, strip time.
WHEN (DATEPART(dw, @StartDate) + @@DATEFIRST) % 7 = 1 then CAST(FLOOR(CAST(DATEADD(DD,1,@StartDate) AS FLOAT )) AS DATETIME) -- 1: Sunday -> forward 1 day, strip time
ELSE @StartDate
END
)
SET @CorrectedEndDate = (
CASE
WHEN (DATEPART(dw, @EndDate) + @@DATEFIRST) % 7 = 0 then CAST(FLOOR(CAST(@EndDate AS FLOAT )) AS DATETIME) -- 0: Saturday -> strip time to reverse to midnight.
WHEN (DATEPART(dw, @EndDate) + @@DATEFIRST) % 7 = 1 then CAST(FLOOR(CAST(DATEADD(DD,-1,@EndDate) AS FLOAT )) AS DATETIME) -- 1: Sunday -> backwards 1 day, strip time to reverse to midnight.
ELSE @EndDate
END
)
SELECT DATEDIFF(mi, @CorrectedStartDate, @CorrectedEndDate)
-(DATEDIFF(wk, @CorrectedStartDate, @CorrectedEndDate) * 2 * 1440)