views:

45

answers:

1

The following code has been created from a different post, but I now have a separate issue. What this code does is basically takes the 2 dates and subtracts weekends from it to get the overall time elapsed in minutes from start to end time excluding weekends. I am now trying to say since there is 11 hours in a work day for me I want to only count 11 hours per day. The results I get are as follows(I added the column names just so you know what they are):

@AllMins  @MinDays
2173         1.50902777777778

Since there are 660 minutes in a work day or 780 minutes not working in a work day I need to subtract this from the overall time. So, 1 day would be 2173-780, but I also need to get the .50902777777778 minutes in the day. Any suggestions?

DECLARE @StartDate DATETIME
DECLARE @EndDate DATETIME
SET @StartDate = '2010-08-02 00:00:00.000'
SET @EndDate = '2010-08-03 12:13:12.123'

--How many minutes are between order start and end time including non working time
DECLARE @AllMins INT 
--Declares how many minutes are in a day and makes it float to get remainder minutes when divided
DECLARE @MinsInDay FLOAT 
SET @MinsInDay = 1440

--Finds how many minutes are between start and end time excluding weekends and assignes to variable 
SET @AllMins = ((DATEDIFF(mi, @StartDate, @EndDate)) 
  -(((DATEDIFF(wk, @StartDate, @EndDate) * 2) * 24) * 60)  
  -(((CASE WHEN DATENAME(dw, @StartDate) = 'Sunday' THEN 1 ELSE 0 END) * 24) * 60)
  -(((CASE WHEN DATENAME(dw, @EndDate) = 'Saturday' THEN 1 ELSE 0 END) * 24) * 60)) 

--Calculates how many days have elapsed in the minutes that the order has taken
DECLARE @MinDays FLOAT
SET @MinDays = (@AllMins/@MinsInDay)

SELECT
@AllMins
,@MinDays
+1  A: 

Are you looking for

@AllMins - (integer portion of (@MinDays) * 780) - (fractional portion of (@MinDays) * 360)

so that you are subtracting the portion of the 780 minutes from the fractional part?

Joe Stefanelli
Well problem is I only want to count only the time from 6:00AM to 5:00PM and have no clue to go from here....
OK, so the fractional part of a day starts at midnight, so you'll want to exclude the 6 hours (360 minutes) between midnight and 6 AM. I've modified my answer to reflect this. Combine this with your other question about splitting out the fractional part of a number and you should be on your way.
Joe Stefanelli