Saturday, July 23, 2011

Adding and subtracting Business Days to a Date


Here is a short function that is designed to add/subtract business days to a date. The parameters to be passed are the number of business days to be added/subtracted (as int), and the date to be added/subtracted to (as datetime). The returned value is a datetime.   The function does not consider holidays as they are different among regions and companies. A holiday in other words is the same as a regular Mon-Friday working day.
SYNTAX
     businessDaysAdd(numbusinessdaysdate)
Here is the complete function definition:

-- ============================================================================
-- Author: spartiatis1975@sqltipsandtricks.com
-- Description: Function that adds or subtracts Business Days from a given date
-- Business days are considered Mon - Fri. Holidays are not considered and are
-- treated as work days.
-- ============================================================================
CREATE FUNCTION [dbo].[businessDaysAdd]
(
@bDays int , -- number of business days to add sub.
@d datetime -- variable to hold the date you want to add sub. days to)
RETURNS datetime
AS
BEGIN

  -- Declare the return variable here
  DECLARE @nDate datetime, @addsub int
  SET @ndate = @d
  IF @bdays > 0
    SET @addsub = 1
  ELSE
    SET @addsub = -1
  WHILE
 @bdays <> 0 -- Keep adding/subtracting a day until @bdays becomes 0
    BEGIN
      SELECT
 @ndate = dateadd(day,1*@addsub,@ndate)
      SELECT @bdays =
      CASE datepart(weekday,@ndate)-- ignore if it is Sat or Sunday        WHEN 1 THEN @bdays
        WHEN 7 THEN @bdays
        ELSE @bdays - 1*@addsub
      END
    END
RETURN
 @nDate
END      
numbusinessdays:
    
 This is the number of business days to be added (possitive int value) or subtracted (negative int value).
date:    The date you add or subtract business days to.
EXAMPLES:
    

    SELECT dbo.businessDaysAdd(-8,’8/17/09′)
    Output: 8/05/09
    SELECT dbo.businessDaysAdd(7,’8/17/09′)
    Output: 8/26/09
NOTE: Use of this function is ‘as is’ with no guarantees what so ever. You are responsible for your own testing.
Please share your comments/bugs/fixes with us.

2 comments:

Jürgen said...

Hi, saved a lot of my time. I've improved it a bit. Using a holyday table and make it independent of @@DATEFIRST

-- ============================================================================
-- Author: spartiatis1975@sqltipsandtricks.com / fassbenderj@gmail.com
-- Description: Function that adds or subtracts Business Days from a given date
-- Business days are considered Mon - Fri. Holidays are not considered and are
-- treated as work days.
-- ============================================================================
ALTER FUNCTION [dbo].[businessDaysAdd] (
@bDays INT -- number of business days to add sub.
,@d DATETIME -- variable to hold the date you want to add or sub days to
)
RETURNS DATETIME
AS
BEGIN
-- Declare the return variable here
DECLARE @nDate DATETIME -- the working date
, @addsub INT -- factor for add or sub

SET @ndate = @d

IF @bdays > 0
SET @addsub = 1
ELSE
SET @addsub = - 1

WHILE @bdays <> 0 -- Keep adding/subtracting a day until @bdays becomes 0
BEGIN
SELECT @ndate = dateadd(day, 1 * @addsub, @ndate) -- incr or decr @ndate

SELECT @bdays = CASE
WHEN (@@datefirst + datepart(weekday, @ndate)) % 7 IN (0, 1) -- ignore if it is Sat or Sunday
THEN @bdays
WHEN ( SELECT 1
FROM Table_2 -- this table holds the holydays
WHERE Datum = @nDate
) IS NOT NULL -- ignore if it is in the holyday table
THEN @bdays
ELSE @bdays - 1 * @addsub -- incr or decr @ndate
END
END

RETURN @nDate
END

Abah Ceprot said...

Are you learning English for Business? Congratulations. Improving your language skills can be a great way to move into a better job or even get the job you've been dreaming of for a long time. But, have you asked yourself this very important question? Exactly what business are you studying for? This is a question many English language learners never really ask! http://smart-detox.web.id/agen/

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by Deep's | Bloggerized by Deep - Deep's Templates | ElearSQL-Server