Results 1 to 9 of 9

Thread: DATEADD function but only for Work Days.

Threaded View

  1. #1

    Thread Starter
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    DATEADD function but only for Work Days.

    I need a SQL function that adds days, but only the workdays. It should exclude holidays, maintainence days/hours, special days etc.
    This is for calculating the estimated finish time of any task being allotted to any employee.

    For the work days, I already have a table which can be created with the following sample code:

    sql Code:
    1. USE tempdb
    2. GO
    3.  
    4. create table WorkSchedule
    5. (
    6. DOW INT,                -- day of week
    7. StartTime DateTime,        -- work start time
    8. EndTime DateTime,        -- work end time
    9. WorkDay bit default 1    -- 1=work day, 0=off day.
    10. )
    11.  
    12. insert into WorkSchedule
    13.       select 1, '1900-01-01 08:00:00', '1900-01-01 17:00:00', 0    -- sunday
    14. union select 2, '1900-01-01 08:00:00', '1900-01-01 17:00:00', 1    -- monday
    15. union select 3, '1900-01-01 08:00:00', '1900-01-01 17:00:00', 1    -- tuesday
    16. union select 4, '1900-01-01 08:00:00', '1900-01-01 17:00:00', 1    -- wednesday
    17. union select 5, '1900-01-01 08:00:00', '1900-01-01 17:00:00', 1    -- thursday
    18. union select 6, '1900-01-01 08:00:00', '1900-01-01 17:00:00', 1    -- friday
    19. union select 7, '1900-01-01 08:00:00', '1900-01-01 17:00:00', 0    -- saturday
    20.  
    21. union select -1, '2011-03-01 08:00:00', '2011-01-03 17:00:00', 0    -- off day on 1-Mar-11
    22. union select -1, '2011-03-15 08:00:00', '2011-03-17 17:00:00', 0    -- off days on 15 to 17th march.
    23. union select -1, '2011-03-25 12:00:00', '2011-03-25 17:00:00', 0    -- half day off on 20-march
    24.  
    25. select * from WorkSchedule
    26. GO

    The table has 7 fixed rows with DOW = 1 to 7 which can't be deleted, only date range or WorkDay=true/false can be set. This is regular schedule and applies to all dates. So for such records, only the Time part is relevant.

    If a record has DOW = -1, it is an exception (either exceptional workday i.e. emergency etc. or a holiday i.e. govt holidays or festivals etc.) and its full date is relevant.

    Now I need a function that takes a current date and minutes to add, and returns me a date with work minutes added to it.
    sql Code:
    1. CREATE FUNCTION AddWorkMinutes
    2. (    @StartDate DATETIME,    -- task start date.
    3.     @MinutesToAdd INT        -- number of minutes alloted.
    4. )
    5. RETURNS DATETIME
    6. AS
    7. BEGIN
    8.     DECLARE @ReturnDate DATETIME
    9.  
    10.     -- calcualate the return date here
    11.  
    12.     -- should work similar to this, but ignore minutes falling in off days.
    13.     -- select @ReturnDate = DATEADD(mi, @MinutesToAdd, @StartDate)
    14.  
    15.     RETURN @ReturnDate
    16. END
    At present, I'm looping through each minute from @StartDate onwards and testing it for positivity, until I have spanned @MinutesToAdd minutes. This is OK for small @MinutesToAdd values, but where say when I need to add a few months, the speed is utterly slow.

    But I think there must be a better way than looping and spanning through each minute. Anyone has any better ideas?
    Last edited by Pradeep1210; Mar 1st, 2011 at 09:38 AM.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width