Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Subtract Business Days from a Specific date

I need to subtract a number of business days (1-15 days) from a specific date, for example subtracting 5 business days from 2013-12-27 should return 2013-12-20, is there an easy way to do that?

like image 264
user3140982 Avatar asked Dec 28 '13 01:12

user3140982


People also ask

How do I subtract business days from a date in Excel?

To calculate workdays in Excel, follow these simple rules: To add workdays, enter a positive number as the days argument of a WORKDAY formula. To subtract workdays, use a negative number in the days argument.

How do I calculate business days from a date in Excel?

The NETWORKDAYS Function[1] calculates the number of workdays between two dates in Excel. When using the function, the number of weekends are automatically excluded. It also allows you to skip specified holidays and only count business days. It is categorized in Excel as a Date/Time Function.

How do I subtract dates in Excel?

You can use the EDATE function to quickly add or subtract months from a date. The EDATE function requires two arguments: the start date and the number of months that you want to add or subtract. To subtract months, enter a negative number as the second argument. For example, =EDATE("9/15/19",-5) returns 4/15/19.

How do I subtract days from a date excluding weekends in Excel?

If you'd like to calculate the difference between two dates while excluding weekends and holidays, use the NETWORKDAYS function instead. This also looks for 3 arguments: the start date, the end date, and optional holidays. Unlike the WORKDAY function, the NETWORKDAYS function does include or count the start day.


3 Answers

One way to do that is to pre-create a table with all the dates for couple of years in advance and select from that table. This way you can mark saturdays, sundays, holidays, etc.

like image 88
Z.D. Avatar answered Oct 05 '22 07:10

Z.D.


This post explains how to do it using a recursive CTE:

Business Days Calc Using Recursive CTE

like image 40
Amjad Avatar answered Oct 05 '22 06:10

Amjad


DECLARE @StartDate DATETIME
DECLARE @EndDate DATETIME
SET @StartDate = '2013/10/01'
SET @EndDate = '2013/10/31'


SELECT
   (DATEDIFF(dd, @StartDate, @EndDate) + 1)
  -(DATEDIFF(wk, @StartDate, @EndDate) * 2)
  -(CASE WHEN DATEPART(dw, @StartDate) = 1 THEN 1 ELSE 0 END)
  -(CASE WHEN DATEPART(dw, @EndDate)   = 7 THEN 1 ELSE 0 END)
                                AS [TotalWorkingDays]

Result

TotalWorkingDays
23

Important Note

This method will only ignore Saturdays and Sundays, If you want to exclude National Holidays and other seasonal Holidays you will need to make use of a calender table as Zdravko Danev has already mentioned.

like image 26
M.Ali Avatar answered Oct 05 '22 07:10

M.Ali