Count number of hours between dates excluding holidays/weekends
Hello all
I havent worked with dates extensively and was recently asked to create a report where Im looking for the number of hours between two dates only counting business days.
So for example I have data as follows
Created 2011-03-30 15:00:00
Processed 2011-03-30 15:03:46
Fulfilled 2011-04-01 17:25:02
Mailed 2011-04-01 17:45:00
For a total of looking from the CREATED and MAILED dates
50 hours 45 minutes
Im also trying to exclude weekends and holidays, i was reading around and was actually able to also find a table of dates where I have the main date, HOLIDAY_IND column and WEEKDAY_IND column
So the calender table i have looks similar to
CALENDER_DATE HOLIDAY_IND WEEKDAY_IND
2011-03-31 Y Y
2011-04-01 N Y
2011-04-02 N N
Im really quite stumped as to where to begin
I was thinking of trying to write it with PL/SQL but i dont have the proper user access to create procedures/functions, so looks like straight up SQL
Any help appreciated!
Hi,
Depeneding on your data and your requirements, you can do something like this:
SELECT created_DATE,
, mailed_date
, 24 * ( (mailed_date - created_date)
SELECT COUNT (*)
FROM table_o_dates
WHERE main_date > created_date
AND main_date < TRUNC (mailed_date)
AND ( holiday_ind = 'Y'
OR weekday_ind = 'N'
) AS hours_between
FROM table_x
I hope this answers your question.
If not, post a little sample data (CREATE TABLE and INSERT statements, relevant columns only) for all tables, and also post the results you want from that data.
Explain, using specific examples, how you get those results from that data.
Always say which version of Oracle you're using.
What do you want to do if created_date or mailed_date is not a work day? Include examples in you data and results.
Similar Messages
-
Get number of hours between two dates and two hours using factory calendar
Hello all,
I have the following requirement: I need to calculate the number of hours between two dates and two hours (start date- finish date and start hour-finish hour) or timestamps using a factory calendar. I must program it on CRM environment.
Does anybody know a function module that makes it?
Thanks in advance.
CarmenPlease check function module DURATION_DETERMINE.
- April King -
Subtracting two timestamps and showing the number of hours between them.
Hello Folks,
What I have is two tables, each of them has a timestamp column. I want to do a select with a join that will show a column with the number of hours between the two times.
(I have added the columns as timestamp(0) since I have no need for the decimals)
Here's what I have:
table one name: conc_test
table one info:
P_DATE-------JOB_NO-----MIX---POUR_NO----AMB_T---CONC_T--SLUMP---AIR---DENSITY---CYL_CAST_TIME
09-MAR-10---9-12-796-----80----100309A------2.5--------16----------135------2.5----2501-------09-MAR-10 10.00.00 AM
table two name: rls_break
table two info:
JOB_NO----POUR_NO-----CYL_ID----SPEC_STR---STR----MIX---BREAK_D_T
9-12-796---100309A----100309A1------20-----------27-----80----10-MAR-10 07.15.00 AM
(I put the --- between the columns so it would be more legible for forum reading, fyi)
This is what I was trying to do:
select
rls_break.cyl_id "Cylinder ID",
rls_break.mix "Mix",
trunc(rls_break.break_d_t-conc_test.cyl_cast_time) "Age",
rls_break.spec_str "Specified Strength",
rls_break.str "Specimen Strength"
from rls_break inner join conc_test on rls_break.pour_no = conc_test.pour_no
where pour_no = '100309A';
I want the "Age" column to show the total number of hours between the cast time (column cyl_cast_time from conc_test) and the break time (column break_d_t from rls_break)
Thank's for any and all help.(I put the --- between the columns so it would be more legible for forum reading, fyi)Please put the tag before and after your examples.
See: http://forums.oracle.com/forums/help.jspa for more information reagarding tags. (scroll a bit down)
I want the "Age" column to show the total number of hours between the cast time (column cyl_cast_time from conc_test) and the break time (column
break_d_t from rls_break)
Do a search on 'NUMTODSINTERVAL' or 'time difference' on this forum, you'll find many related examples. -
Hello -
I've been tearing my hair out over this problem i'm trying to solve, probably just been staring at it too long which is making it worse -
I have a series of open support tickets which are supposed to be updated on a daily basis, the problem is that they aren't always being updated daily. So, the business wants to know the number of days from when a ticket was last updated and today's
date. I have this basic calculation and it's working fine, however now the business wants to exclude weekends from the calculation. The other problem is that some reps DO go in on weekends and update their tickets, so sometimes there will be updates
made on weekend dates.
To give an example -
Today's date is 2014-02-10 (Monday). A ticket was last updated last Thursday, 2014-01-30. The difference between the two dates is 11, so it's been 11 days since the ticket was last updated. Now, if I exclude Saturdays and Sundays, then
it's actually been 7 days since the ticket was last updated. I'm not sure how to do this in T-SQL.
Now, to further complicate the problem, sometimes a ticket IS updated on a Saturday or Sunday. So, if a ticket was updated on 2014-02-02 (Sunday), then it should be counted. Again i'm not sure how to do this.
What gets me is that this is probably fairly simple and i've just been staring at it too long. In the meantime, can someone offer some guidance?
Thanks!!I've adapted this from a function on my blog. you will need to add set the YourTicketTable to where ever your tickets are stored.
CREATE
FUNCTION [dbo].[CalcWorkDaysBetween](@StartDate
As DateTime,@EndDate
AS DateTime)
RETURNS
INT AS
BEGIN
SET @EndDate
=DATEADD(DAY,1,@EndDate)
DECLARE @Count
AS Int= 0
DECLARE @Date
As Date=@StartDate
WHILE @Date
< @EndDate
BEGIN
IF (DATEPART(WEEKDAY,@Date)IN(1,7)
OR (SELECT
Count(*)
FROM YourTicketTable WHERE TicketDate=@Date)=1)
BEGIN
SELECT @Count = @Count
+ 1
END
SELECT @Date=DATEADD(Day,
1,@Date)
END
RETURN
DATEDIFF(DAY,@StartDate,@EndDate)- @Count
END
Regards, -
How to calculate Number of Hours between 2 dates
Hi,
I have a Column in a table of datatype DATE i.e. Update_Date. Now How do I calculate number of hours passed starting from this date entered in the Update_Date column to SYSDATE. e.g. if the date in column is 2-FEB-2009 and the sysdate is 2/2/2009 2:07:40 AM it should give me 24. How can I get this number of hours.
Update_Date - Sysdate = Number of Hours.
ThanksWhen you subtract two dates in Oracle, you get a difference in terms of days. Just multiply that by 24 to get hours.
SELECT (sysdate - update_date) days,
(sysdate - update_date) * 24 hours
FROM your_tableThat said, it is not obvious how your example works. You state
if the date in column is 2-FEB-2009 and the sysdate is 2/2/2009 2:07:40 AM it should give me 24Since both dates are on February 2, 2009, the only difference is in the time component. If we assume that the time component of the UPDATE_DATE value is midnight since it is not specified, there is a little more than 2 hours of difference between the two dates. How is it that you determine there are 24 hours of difference?
Justin
Edited by: Justin Cave on Feb 2, 2009 10:39 AM -
Time between dates excluding Weekends and Holidays
I am fairly new to Power Pivot and I am having trouble with this formula. I need my Cycle Time Days column to capture the days between CreatedDataTime and ClosedDateTime excluding Weekends and Holidays. I have a tab that has the holidays but
I can't get the formula correctly? Jill
CreatdDateTime ClosedDateTime Cycle Time Days
12/1/2014 12/10/2014first of all you need a separate time table which holds all possible dates for your model
there should be no gaps, it should contain full years and a column of datatype DateTime as unique key
once you have this in place add a calculated column to this table as
=IF(INT(FORMAT([Date], "w")) >= 6, "Weekend", "Workday")
then go back to your original table and add the following calculation for your [Cycle Time Days]-column:
=COUNTROWS(
FILTER(
'Dates',
Dates[Date] >= DateRanges[DateFrom]
&& Dates[Date] <= DateRanges[DateTo]
&& 'Dates'[IsWeekend] = "Workday"
Holidays are a bit more tricky but you can follow the same pattern but you need to get the holidays from somewhere
hth,
-gerhard
Gerhard Brueckl
blogging @ http://blog.gbrueckl.at
working @ http://www.pmOne.com -
How to count number of sundays between two dates
Hi
I want number of Sundays between two dates
example
number of Sundays count between '01-04-2013' and '30-04-2013' in one select query I have to include this as sub query in my select statement.Hi,
ChakravarthyDBA wrote:
Hi
I want number of Sundays between two dates
example
number of Sundays count between '01-04-2013' and '30-04-2013' in one select query I have to include this as sub query in my select statement.Here's one way:
SELECT early_date
, late_date
, ( TRUNC (late_date + 1, 'IW')
- TRUNC (early_date, 'IW')
) / 7 AS sundays
FROM table_x
;This does not depend on your NLS settings.
I hope this answers your question.
If not, post a little sample data (CREATE TABLE and INSERT statements, relevant columns only), and also post the results you want from that data.
Point out where the statment above is getting the wrong results, and explain, using specific examples, how you get the right results from the given data in those places.
Always say which version of Oracle you're using (e.g., 11.2.0.2.0).
See the forum FAQ {message:id=9360002} -
SLA Calculation needed to exclude Holidays, Weekends and Off business hours
Hello all,
I am to create a function that will calculate the minutes between two dates for company work hours, excluding the weekends.
The function here from RSingh() is ideal for this purpose; however I am now asked to excluded company holidays also.
http://social.msdn.microsoft.com/Forums/sqlserver/en-US/de4f7661-c702-4a10-ad06-3e28e4e0a83c/sla-calculation-help-needed-in-excluding-weekends-and-off-business-hours-for-calculation?forum=transactsql
With a table called 'holidays' that contains holiday dates. I would hope to modify the function to exclude these.
Please, how can this be accomplished?My approach would be something like this
Populate a calendar table that includes a work start and work end time for each day (none for weekends and holidays)
SELECT *
INTO #cal
FROM (
SELECT CAST('20140117' AS DATE) AS CalendarDate
, CAST('20140117 09:00' AS DATETIME) WorkStart
, CAST('20140117 17:00' AS DATETIME) AS WorkEnd
UNION ALL
SELECT CAST('20140118' AS DATE) AS CalendarDate
, CAST(NULL AS DATETIME) WorkStart
, CAST(NULL AS TIME) AS WorkEnd
UNION ALL
SELECT CAST('20140119' AS DATE) AS CalendarDate
, CAST('20140119 09:00' AS DATETIME) WorkStart
, CAST('20140119 17:00' AS DATETIME) AS WorkEnd
UNION ALL
SELECT CAST('20140120' AS DATE) AS CalendarDate
, CAST(NULL AS DATETIME) WorkStart
, CAST(NULL AS TIME) AS WorkEnd
UNION ALL
SELECT CAST('20140121' AS DATE) AS CalendarDate
, CAST(NULL AS DATETIME) WorkStart
, CAST(NULL AS TIME) AS WorkEnd
UNION ALL
SELECT CAST('20140122' AS DATE) AS CalendarDate
, CAST('20140122 09:00' AS DATETIME) WorkStart
, CAST('20140122 17:00' AS DATETIME) AS WorkEnd
) X
Use a query something like the following to work out the number of minutes
DECLARE @Start DATETIME = '20140117 13:00'
DECLARE @End DATETIME = '20140122 17:00'
SELECT SUM(diff)
FROM (
SELECT DATEDIFF(MINUTE, CASE WHEN CAST(@Start AS DATE) = CalendarDate
AND @Start > WorkStart THEN @Start ELSE WorkStart END, CASE WHEN CAST(@End AS DATE) = CalendarDate
AND @End < WorkEnd THEN @End ELSE WorkEnd END) AS diff
FROM #cal
WHERE CalendarDate BETWEEN CAST(@Start AS DATE)
AND CAST(@End AS DATE)
) X
Edit: Totally mucked up the example calendar table so I've changed it -
Hello, I have a problem to obtain the number of hours passed between two dates type dd/mm/yy hh:mm:ss Since of all the day I only must consider the time passed between the 09:00 and the 19:00 of Monday through Friday. Can somebody help me? Thanks.
static final int MILLIS_PER_HOUR = 3600000;
static final int START_OF_DAY = 9 * MILLIS_PER_HOUR;
static final int END_OF_DAY = 19 * MILLIS_PER_HOUR;
public static double hoursBetween(Date d1, Date d2) {
long midnight = /* 00:00 of the same day */.getTime();
long msSinceMidnight1 = d1.getTime() - midnight;
long msSinceMidnight2 = d2.getTime() - midnight;
// if times fall in "non-countable" hours, adjust them
if (msSinceMidnight1 < START_OF_DAY) {
msSinceMidnight1 = START_OF_DAY;
if (msSinceMidnight2 > END_OF_DAY) {
msSinceMidnight2 = END_OF_DAY;
return (double)(msSinceMidnight2 - msSinceMidnight1) / MILLIS_PER_HOUR; -
Auto Counting Number of Rows Where Data is Entered
I am trying to automatically count the number of rows where data is entered. For example, I have a table with six open rows to enter information. If only three of those rows (A2-A4) had names entered in them, and the next three rows were left blank, can I get each name to equal the value "1" so that I can get a total tally of how many names have been entered? So the final tally in this example would be "3." I appreciate your help!
file:///Users/josephdlugo/Desktop/Screen%20Shot%202014-04-30%20at%2011.58.28%20A M.pngthere is a function, COUNTA(), that will count the number of items that are not empty:
C1=COUNTA(B)
this is shorthand for select cell C1, then type, or copy and paste the formula:
=COUNTA(B)
The COUNTA function returns the number of its arguments that are not empty.
COUNTA(value, value…)
value: Any value or a collectioncontaining any values.
value…: Optionally include one or more additional values or collections. -
Function Module for calculating hours between dates and their times.
Hi all,
Is there any standard function module for calculating the time difference in hours between two dates with start and end times.
Regards,
Sudipto.Sudipto,
How're you ????????????????????
Check out the following Function Module:
SD_DATETIME_DIFFERENCE (Give the difference in Days and Time for 2 dates)
Regards,
Abir
Don't forget to award Points * -
Finding difference of Hours between two dates
I need logic to find the difference of number of hours between any two dates excluding saturdays & sundays. Please provide PLSQL code.
my dear
this is the pl/sql to create on db.
| | This function it will created on the data if you like.
| | the input parameter for this function is two date .
| | you can add more feature on it because turn it felixable eg.
| | you can execlude varaible days from paramteres....
CREATE OR REPLACE FUNCTION GET_HOURS(P_FROM_DATE IN DATE ,
P_TO_DATE IN DATE DEFAULT SYSDATE ,
P_EXECLUDE IN VARCHAR2 DEFAULT '17' ) RETURN NUMBER IS
V_CURR_DATE DATE := P_FROM_DATE ;
V_ALL_DAYS NUMBER := 0 ;
V_NET_HOURS NUMBER := 0 ;
V_FROM_JUL NUMBER := TO_CHAR(P_FROM_DATE,'J');
V_TO_JUL NUMBER := TO_CHAR(P_TO_DATE,'J');
BEGIN
FOR R IN V_FROM_JUL..V_TO_JUL LOOP
IF INSTR(P_EXECLUDE,TO_CHAR(V_CURR_DATE,'D')) = 0 THEN
V_ALL_DAYS := V_ALL_DAYS + 1 ;
END IF;
V_CURR_DATE := V_CURR_DATE+ 1 ;
END LOOP;
V_NET_HOURS := V_ALL_DAYS * 24 ;
RETURN(V_NET_HOURS);
END;
-- this is for test senario
SELECT GET_HOURS(SYSDATE-30) FROM DUAL ;
SELECT GET_HOURS(SYSDATE-30,SYSDATE,'127') FROM DUAL;
SELECT GET_HOURS(SYSDATE-30,SYSDATE,'0') FROM DUAL; -- to execlude zero days. -
I'm trying to create a variable that contains the number of hours between the item created date and Now. When i use TODAY, it seems to set the time at 12:00 rather than the time now - so that throws the hours off. Any ideas?
ajwHi ajw,
According to your description, my understanding is that you want to calculate the hours between the item created date and now.
It seems to be not an OOB way to implement your requirement, to get the current time, you need to create a custom workflow activity for SharePoint 2010 workflow. About creating custom workflow activity, you can refer to the links below:
http://msdn.microsoft.com/en-us/library/hh872790(v=office.14).aspx
http://blogs-sharepoint-ashutoshmisra.blogspot.in/2013/02/create-custom-workflow-action-for.html
Or, you can use a third party solution to achieve your requriement.
Here are some similar posts for you to take a look at:
http://social.technet.microsoft.com/Forums/sharepoint/en-US/e93ea37a-df09-4cbf-a58d-5d4def3d3d42/how-to-compare-time-in-sharepoint-designer-2010sp-2010-workflow?forum=sharepointgeneralprevious
http://blog-sharepoint.blogspot.in/2009/03/get-current-time-in-sharepoint-designer.html
I hope this helps.
Thanks,
Wendy
Wendy Li
TechNet Community Support -
How to get difference in days between 2 dates excluding weekends
Hi all,
i have a requirement, to calculate the difference between 2 dates, in days.
eg: 01/08/2007 and 05/08/2007.
Difference = 4 days.
But here my actual requirement is i have to calculate this difference excluding weekends (saturday n sundays).
eg: 01/08/2007 -
Thursday
05/08/2007 -
Monday
so now Difference = 2 days.
Please help me regarding this.
Points will be rewarded for helpfull answers.
Thanks in Advance.
Regards,
Vineelsee these codes of rich
report zrich_0003.
data: begin of itab occurs 0,
datum type sy-datum,
end of itab.
data: weekday like dtresr-weekday.
data: number_lines type i.
parameters: p_sdatum type sy-datum,
p_edatum type sy-datum.
itab-datum = p_sdatum.
append itab.
do.
if itab-datum = p_edatum.
exit.
endif.
itab-datum = itab-datum + 1.
call function 'DATE_TO_DAY'
exporting
date = itab-datum
importing
weekday = weekday.
if weekday = 'Sat.'
or weekday = 'Sunday'.
continue.
endif.
append itab.
enddo.
describe table itab lines number_lines.
write:/ 'Number of days between dates is', number_lines.
and
report zrich_0001.
parameters: p_start type sy-datum,
p_end type sy-datum.
data: idays type table of rke_dat with header line.
data: workingdays type i.
call function 'RKE_SELECT_FACTDAYS_FOR_PERIOD'
exporting
i_datab = p_start
i_datbi = p_end
i_factid = 'P8' " Fact Calender ID
tables
eth_dats = idays
exceptions
date_conversion_error = 1
others = 2.
describe table idays lines workingdays.
write:/ workingdays.
I want to find the No.of working days between the two dates
regards,
srinivas -
Number of days between 2 dates
HI,
I am working on a program that needs to show number of days between 2 dates. When I scanned the function library, I only found a function to give you the number of years between dates. I can probably code this in ABAP but does anyone know if a function exists to do this.Hi Suneela
Are you concern with the normal dates or do you have any concern with the Factory dates ?
Lets take both
1) Normal dates
It seems that you are trying to get the days between two dates , That you can simply get by subtracting one from the other or else by .
Assuming that you populated the dates just after the declaration .
Sample code
Data : date1 like sy-datum,
Date2 like sy-datum ,
No_of_days type i.
No_of_days = date1 date2.
Or Else the FM
Days_between_two_dates
Sample code
Data : date1 like sy-datum,
Date2 like sy-datum ,
No_of_days type i.
Call function days_between_two_dates
Exporting
I_datum_bis = Date2 Second date
I_datum_von = date1 Start date
Importing
E_tage = No_of_days
Exceptions
Days_method_not_defined = 1
Others = 2.
2) Factory dates
If You want to find out the difference based on factory calendar
Then convert the dates to factory calendar dates first and then simply subtract those factory calendar dates to get offset days based on factory calendar.
*Sample code *
Data : date1 like sy-datum,
Date2 like sy-datum ,
No_of_days type i.
data: l_fac_date like SCAL-FACDATE , " Factory calendar: Factory
l_fac_date2 like SCAL-FACDATE,
l_fac_date1 like SCAL-FACDATE.
Perform get_factory_date using date1
Changing l_fac_date1.
Form get_factory_date using l_date
Changing l_l_fac_date.
IF NOT l_date1 IS INITIAL.
CALL FUNCTION 'DATE_CONVERT_TO_FACTORYDATE'
EXPORTING
CORRECT_OPTION = '+'
DATE = l_date
FACTORY_CALENDAR_ID = 'AU' "
IMPORTING
DATE =
FACTORYDATE = l_l_fac_date
WORKINGDAY_INDICATOR =
EXCEPTIONS
CALENDAR_BUFFER_NOT_LOADABLE = 1
CORRECT_OPTION_INVALID = 2
DATE_AFTER_RANGE = 3
DATE_BEFORE_RANGE = 4
DATE_INVALID = 5
FACTORY_CALENDAR_NOT_FOUND = 6
OTHERS = 7
IF SY-SUBRC <> 0.
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
ENDIF.
Endform.
Move l_fac_date to l_fac_date1.
*Do the same for the second date also i.e .
Perform get_factory_date using date2
Changing l_fac_date2.
No_of_days = l_fac_date1 - l_fac_date2.
Hope this will help you.
Reward points if helpful
Maybe you are looking for
-
I am from Bangladesh. I can't log in with my gmail Apple ID. When I put in the mail and password, and press sign in, it says it has to review on iTunes. Then I am directed to opening a new account. But why should I , since I have one. Plz help
-
Error in RAR while approving request from CUP
Dear GRC Gurus, I am getting error while approving request in CUP for RAR. Checked the related threads for this issue but still not getting any solution. Connectors are working fine. Also web service URL is maintained correctly and password for the s
-
MacBook suddenly can't connect to wifi
I have been using the same wifi at home for three years, and suddenly today I am not able to connect. My iPad and phone are all able to connect to the wifi except my laptop. It started when I was in queue for playing League of Legends, but I had to f
-
Mass deletion of Batch specific unit of measure
Dear Experts, How to delete the batch specific unit of measure in the material master maintained in the additional data -unit of measure and proportional / production unit of measure in mass . We have some 1000 material for which we want to remove th
-
I installed Adobe Web and Design Preium CS6 on windows 8.1, but when I open the program the size of the menus and tools are so tiny. It is not like that for Acrobat, but all the other programs. Is there a fix for this?