I'm trying to calculate the difference between two dates excluding the weekends and only count the time from 8pm - 6am. I want to calculate that difference in Days, Hours and Minutes.
For that I have this:
DECLARE @Start_Date DATETIME
DECLARE @End_Date DATETIME
SET @Start_Date = '2017-06-23 10:43:41.000'
SET @End_Date = '2017-06-27 11:58:52.000'
SELECT (DATEDIFF(dd, @Start_Date, @End_Date) + 1)
-(DATEDIFF(wk, @Start_Date, @End_Date) * 2)
-(CASE WHEN DATENAME(dw, @Start_Date) = 'Sunday' THEN 1 ELSE 0 END)
-(CASE WHEN DATENAME(dw, @End_Date) = 'Saturday' THEN 1 ELSE 0 END) AS [Time to First Atualization- Days],
datediff(hour, @Start_Date, @End_Date) - (datediff(wk, @Start_Date, @End_Date) * 48) -
case when datepart(dw, @Start_Date) = 1 then 1 else 0 end +
case when datepart(dw, @End_Date) = 1 then 1 else 0 end AS [Time to First Atualization- Hours],
datediff(minute, @Start_Date, @End_Date) - (datediff(wk, @Start_Date, @End_Date) * 2880) -
case when datepart(dw, @Start_Date) = 1 then 1 else 0 end +
case when datepart(dw, @End_Date) = 1 then 1 else 0 end AS [Time to First Atualization- Minutes]
The number of days the query return the correct value but to calculate the number of hours and minutes it's wrong...
How can I solve this?
Thanks!