November 8, 2007 at 12:33 am
hi Everyone,
I have one query, i want to calculate the business working hours (like from monday to friday 9 to 5 pm and saturday 9 to 2 pm) for the given 2 dates. And also i want to eliminate the public holidays with in the given dates.
If any one was faced the same situation plz help me.
Regards
Venki
November 8, 2007 at 1:21 am
I think there already are about 15 zillion postings about this exact problem.
Try to search this forum for similar requests and see what you get?
N 56°04'39.16"
E 12°55'05.25"
November 8, 2007 at 2:40 am
i got one example, but it is useful only for calculating the number of working days.
url is: http://weblogs.sqlteam.com/brettk/archive/2005/05/12/5139.aspx
If anyone knows my query plz respond me.
Its very urgent.
November 8, 2007 at 8:47 am
You may find your way to your answers here:
/Kenneth
November 8, 2007 at 9:08 am
well - let's get some specifics before we go diving into weeks and weeks of back and forth (well - probably will happen anyway.....:D)
Jeff Moden took a running start at whole days in a recent article. [/url]
- What do you plan on passing in? Are we dealing with specific work time starts and stops (e.g. 9/1/07 10:20am to 10/15/07 1:55pm) or full days? Are they passed in combined, or separated? (e.g. Start datetime, end datetime vs start date, start time, end date, end time)
- what - no lunch breaks?
- where are you storing your holidays?
----------------------------------------------------------------------------------
Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?
November 12, 2007 at 12:03 am
We are storing holidays list in seperate table. And also storing the Business hours in that table.
We are storing the data in datetime format. I want to calculate the hours and minutes in between the passed dates(datetime format).
Give me solution Plz
Thanks & Regards
Venki
November 12, 2007 at 1:44 am
Ok, could you then please give an example on what this table of yours look like?
A few rows of sample data and the expected output you need from that data.
/Kenneth
November 12, 2007 at 2:21 am
For this i am using 2 tables i.e., Holidays and WorkingHours. We are storing only holidays dates in holidays table. in working hours table 3 columns were there i.e., weekday, starting time, closing time.
we are storing the values like from monday to friday 9 am to 5 pm and for saturday 9 am to 1 pm.
thanks & regards
Venki
November 27, 2007 at 9:39 am
Hi Venki,
Here is the answer to your problem.
http://www.sqlservercentral.com/scripts/Scalar-valued+Functions/61567/
As I understand from your description, you need to do the following modifications to get what you exactly need.
1. The function CalcTimeSinceCreated will have to be modified to take two parameters case as 'task beginning date&time' and 'task ending date&time', instead of one at present
2. I am hardcoding the working hours and weekdays in the functions. You need to modify it to take this information from 2 tables i.e., Holidays and WorkingHours.
If you provide me the exact structure of your two tables and a few rows of the sample data then I may help you in the above two tasks.
Regards,
Mazharuddin
-----------------------------------------------------------[font=Arial Black]Time Is Money[/font][font=Arial Narrow]Calculating the Number of Business Hours Passed since a Point of Time[/url][/font][font=Arial Narrow]Calculating the Number of Business Hours Passed Between Two Points of Time[/font]
November 28, 2007 at 9:27 pm
HI Mazharuddin
I AM VERY MUCH THANKFUL TO YOU FOR YOUR SOLUTION.
I AM WORKING ON THAT NOW.
ONCE AGAIN THANKS
REGARDS
VENKI
November 29, 2007 at 4:26 am
Hi Venki,
It is my great pleasure to know that the script is helpful for your requirements. Please post your progress and/or difficulties if any to this forum or at
Also I will be thankful if you rate the above script for its contents and usefulness.
Regards,
Mazharuddin
-----------------------------------------------------------[font=Arial Black]Time Is Money[/font][font=Arial Narrow]Calculating the Number of Business Hours Passed since a Point of Time[/url][/font][font=Arial Narrow]Calculating the Number of Business Hours Passed Between Two Points of Time[/font]
January 14, 2008 at 11:41 am
Hi Venki,
It is my pleasure to inform that I have posted the second version of my work
Calculating the Number of Business Hours Passed Since a Point of Time[/url]
I have modified it to
Calculate the Number of Business Hours Passed BetweenTwo Points of Time
The new version will will take two datetime type value as input parameters ('Start Date' & 'End Date' ). It also incorporates holidays.
Sincerely,
Maz
-----------------------------------------------------------[font=Arial Black]Time Is Money[/font][font=Arial Narrow]Calculating the Number of Business Hours Passed since a Point of Time[/url][/font][font=Arial Narrow]Calculating the Number of Business Hours Passed Between Two Points of Time[/font]
January 14, 2008 at 11:02 pm
hi Maz,
It was very useful to my function. Its working yaar.
Thanks for giving me great support
Yours sincerely
Venki
July 26, 2009 at 2:04 pm
Thought I'd in throw an update to this problem...
If I may suggest...
1. We don't need RBAR to do this... no WHILE loops please. :sick:
2. We don't need 8 functions to do this... that's a lot of complicated code to maintain not to mention the overhead of calling 8 functions.
3. We certainly don't need any hardcoding of times for this. Even the DatePart(DW) shouldn't be hardcoded because of the possible settings of DATEFIRST
4. The absolute best way to do this is to have a Calendar table. Yeah, I know... for some reason, lots and lots of folks would rather use some very complicated code and While Loops to do this instead of the very simple method of using a Calendar table.
With all of that in mind, might I suggest the following instead?
--===== Declare some obviously named variables
DECLARE @StartDateTime DATETIME,
@EndDateTime DATETIME,
@WorkTimeStart1 DATETIME,
@WorkTimeEnd1 DATETIME,
@WorkTimeStart2 DATETIME,
@WorkTimeEnd2 DATETIME,
@BinSize INT,
@Saturday INT, --Datepart(dw) for Saturday regardless of DATEFIRST
@Sunday INT --Datepart(dw) for Sunday regardless of DATEFIRST
;
--===== Preset the variables
SELECT @StartDateTime = '2007-11-16 15:30', --Likely parameter in function
@EndDateTime = '2007-11-20 14:00', --Could be parameter in function
@WorkTimeStart1 = '07:30', --Could be parameter in function
@WorkTimeEnd1 = '11:30', --Could be parameter in function
@WorkTimeStart2 = '12:00', --Could be parameter in function
@WorkTimeEnd2 = '16:00', --Could be parameter in function
@BinSize = 15, --Minutes, Could be parameter in function
@Saturday = DATEPART(dw,5), --First Saturday of 1900
@Sunday = DATEPART(dw,6) --First Sunday of 1900
;
--===== Using the start and end time, calculate the number of business hours
-- between those two date/times.
WITH
cteTimeSlots AS
(--==== Produces a list of datetime slots in @BinSize minute intervals
SELECT DATEADD(mi,(t.n-1)*@BinSize,@StartDateTime) AS TimeSlot
FROM dbo.Tally t
WHERE t.N = @WorkTimeStart1 AND d.Time = @WorkTimeStart2 AND d.Time < @WorkTimeEnd2)
)
-- AND NOT EXISTS (SELECT 1 FROM dbo.Holiday h WHERE d.Date = h.Date)
As usual, details of how it works are in the comments. If you don't know what a Tally table is or how it works, please see the following article...
[font="Arial Black"]http://www.sqlservercentral.com/articles/T-SQL/62867/[/font][/url]
--Jeff Moden
Change is inevitable... Change for the better is not.
December 11, 2009 at 9:03 am
This is my first experience with a tally table. I modified your solution to calculate number of business days given the start datetime and end datetime, and it worked perfectly. Amazingly elegant and fast.
Viewing 15 posts - 1 through 15 (of 28 total)
You must be logged in to reply to this topic. Login to reply