Here is the code listing:
As I said, pretty straight forward. Since SQL, at least in Microsoft SQL Server, will treat division of int datatypes as integer division, you will get the whole number amount of weeks represented by work days (i.e. 8/5 = 1). For other platforms, just use FLOOR function or its equivalent in your code (e.g. Floor(@workDays / 5) or the integer division operator to achieve the same results. Subsequently, modulus is taken of working days to get the partial week days involved which should be 0 - 4 that we can add to calendar days we calculated from multiplying the number of weeks by 7.
DECLARE @workDays int, @calDays int
SET @workDays = 3
-- using integer division to convert work weeks to calendar weeks
-- AND modulus division to get partial week's days
SELECT @calDays = @workDays / 5 * 7 + @workDays % 5
-- just double check that end result is not on a weekend
WHILE DatePart(dw, DateAdd(dd, @calDays, getdate())) IN (7, 1)
SET @calDays = @calDays + 1
-- select away you have your calendar days and date if you would like
SELECT DateAdd(dd, @calDays, getdate())
You can just run in query window as above, but to complement our function/procedure to get working days from a set of calendar dates you can put this in a stored procedure or user defined function the returns the calendar days themselves or the resulting date.
Keep evolving development!
2 comments:
For negative work days capability, the following line:
SET @calDays = @calDays + 1
Should be changed to:
-- get sign of working days and add
-- should be -1 or +1
SET @calDays = @calDays + Sign(@workDays)
If you need to do this inline:
<code>
SELECT DateAdd(dd, (@workDays / 5 * 7 + @workDays % 5) +
Case DatePart(dw, DateAdd(dd,(@workDays / 5 * 7 + @workDays % 5), getdate()))
When 1 Then Sign(@workDays) * 2
When 7 Then Sign(@workDays) * 1
Else 0
End, DateDiff(dd, 0, getdate()))
</code>
Post a Comment