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.
Similar Messages
-
Calculate Difference in Days Between two Dates
Hi,
I'm trying to figure out how to calculate the difference in days between two dates using JavaScript in LiveCycle. (JavaScript knowledge = minimal)
Where "Start_Date" and "Current_Date" are the names of the two dates in the Hierarchy palette. (both Date/Time Field)
*Current date is using the Object > Value > Runtime Property > Current Date/Time
I need a Text or Numeric field displaying the difference in days. (Difference_in_Days)
I've noticed the following code being pretty standard amongst other responses:
var
Start_Date = new Date(Start_Date);
var
Current_Date = new Date(Current_Date);
var
nAgeMilliseconds = Current_Date.getTime() - Start_Date.getTime();
var
nMilliSecondsPerYear = 365 * 24 * 60 * 60 * 1000;
I know there's code missing, and the above code might not be correct.
Please advise.Where "DateField01" = user entered date field
Where "DateFiled02" = Current Date/Time (Runtime Property)
where "Subform" = the subform containing DateField01
My script now resembles:
var oneDay = 24*60*60*1000;
var firstDate = new Date(Subform.DateField01.rawValue);
var secondDate = new Date(DateField02.rawValue);
(firstDate.getTime() - secondDate.getTime()) / oneDay;
I tried adding:
app.alert(String(diffDays));
Although I assume the reason I didn't get an error is because DateField01 is empty when the form is opened.
If I swap in actual dates instead of fields it works perfectly.
When I use the fields I have no information populating after I enter a date in "DateField01" -
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 -
Function module to find no of days between two dates
hi everybody i want a function module to find no of days between two dates
Function module /SDF/CMO_DATETIME_DIFFERENCE
Uppercase/Lowercase
Runtime: 59,629 Microseconds
Import parameters Value
DATE1 01.01.2007
TIME1 00:00:00
DATE2 31.12.2007
TIME2 00:00:00
Export parameters Value
DATEDIFF 364 TIMEDIFF 0
EARLIEST 1 -
How to find difference in amount between two fields in two different tables
Hi,
I am new to PL/SQL. I need to write a procedure to calculate the difference between two columns of two seperate tables. For example:
I have Table a and Table b. Both with two columns Id and Amount. I need to find the difference of Amount between table a and table b value for each record in table b, store it in variable and then do further operations using that difference. Kindly help me with this.Something like this?
INSERT INTO table_c (id, diff, p_or_n)
SELECT
a.a_id,
a.a_value - b.b_value
DECODE( SIGN(a.a_value - b.b_value),
1 , 'P',
0 , 'P',
-1, 'N'
FROM table_a a JOIN table_b b
ON (a.a_id = b.b_id);
-- Ranit -
Find No Of Days Between Two Dates
hello and hi
me have a data base given below
me want to find no of days between '16-oct-2008' and '02-NOV-2008'
Emp Start_Date End_Date
25551 10/16/2008 10/30/2008
25551 10/31/2008 11/30/2008
any body help me pls
thanksOk, 2 points:-
1. Simply subtract the two dates and use ROUND
2. This is an Oracle Forms forum, post query related doubts on SQL/PLSQL forum -
Help with hours between two dates
Hi I need help with the below query
select count(*),a.col1,max(a.dt),min(a.dt)
from tab1 a
where a.dt > to_date('01-JUL-08','dd-mon-yyyy')
group by col1
having count(*) >=2
and (max(a.dt) - min(a.dt))* 24*60*60 > 360;
I am trying to pull the records with more than 2 entries, and having more than 6 hrs difference between max date and min date. When I am trying to execute this I am getting an error at "(max(a.dt) - min(a.dt))* 24*60*60 > 360".
any ideas how can I pull the records more than 6 hrs?
Thanks in advance!Sorry guys...here is the erros i am getting
ORA-00932: inconsistent datatypes: expected INTERVAL DAY TO SECOND got NUMBER
my version : 9.2
SQL>
SQL> @ver
BANNER
Oracle9i Enterprise Edition Release 9.2.0.8.0 - 64bit Production
PL/SQL Release 9.2.0.8.0 - Production
CORE 9.2.0.8.0 Production
TNS for Solaris: Version 9.2.0.8.0 - Production
NLSRTL Version 9.2.0.8.0 - Production
SQL>
SQL>
SQL> --
SQL> drop table tab1;
Table dropped.
SQL> create table tab1 as
2 select 'stin_8723646' as col1, to_date('7/22/2008 5:48:32 AM', 'mm/dd/yyyy hh:mi:ss AM') as dt from dual union all
3 select 'stin_8723646', to_date('7/22/2008 1:34:12 PM', 'mm/dd/yyyy hh:mi:ss AM') from dual union all
4 select 'stin_8723646', to_date('7/22/2008 3:16:25 PM', 'mm/dd/yyyy hh:mi:ss AM') from dual union all
5 select 'stin_8723647', to_date('7/22/2008 1:10:07 AM', 'mm/dd/yyyy hh:mi:ss AM') from dual union all
6 select 'stin_8723647', to_date('7/22/2008 4:19:00 AM', 'mm/dd/yyyy hh:mi:ss AM') from dual union all
7 select 'stin_8723647', to_date('7/22/2008 9:52:44 AM', 'mm/dd/yyyy hh:mi:ss AM') from dual;
Table created.
SQL>
SQL> alter session set nls_date_format = 'mm/dd/yyyy hh:mi:ss am';
Session altered.
SQL>
SQL> select * from tab1;
COL1 DT
stin_8723646 07/22/2008 05:48:32 am
stin_8723646 07/22/2008 01:34:12 pm
stin_8723646 07/22/2008 03:16:25 pm
stin_8723647 07/22/2008 01:10:07 am
stin_8723647 07/22/2008 04:19:00 am
stin_8723647 07/22/2008 09:52:44 am
6 rows selected.
SQL>
SQL> --
SQL> select
2 count(*),
3 a.col1,
4 max(a.dt),
5 min(a.dt)
6 from tab1 a
7 where a.dt > to_date('01-JUL-08','dd-mon-yyyy')
8 group by col1
9 having count(*) >=2
10 --and (max(a.dt) - min(a.dt))* 24*60*60 > 360
11 and (max(a.dt) - min(a.dt))* 24 > 6;
COUNT(*) COL1 MAX(A.DT) MIN(A.DT)
3 stin_8723646 07/22/2008 03:16:25 pm 07/22/2008 05:48:32 am
3 stin_8723647 07/22/2008 09:52:44 am 07/22/2008 01:10:07 am
SQL>
SQL>isotope -
Calculate difference of days between two dates
Hi,
Date1: It is a mandatory type-in date at the time of query execution. How to create it? By default, it should take today's calendar date.
Date2: it is a date in a Char format. (0NETDUEDATE) in one of the dimension of InfoCube.
In query column
of days = Date1 - Date2.
I would prefer to use in global variable.
Please help.
~SHello ,
For date 1 create a characteristic variable(ready to input) on 0calday with customer exit as processing type and key in the following code to get the system date.
data aktdat like sy-datum.
when 'test'.
aktdat = sy-datum(6).
clear l_s_range.
l_s_range-low = aktdat.
l_s_range-sign = 'I'.
l_s_range-opt = 'EQ'.
append l_s_range to e_t_range.
Then create a user entry variable for date 2 using the characteristic mentioned,
then for calculating the days difference check the following.
http://www.sd-solutions.com/documents/SDS_BW_Replacement%20Path%20Variables.html -
Find the difference between two dates for the specific month and year
Hi,
I have two dates, start date is 30/12/2012 and end date is 04/01/2013. Using datediff I found the difference of days between two dates. But I find the no of days in January 2013. ie output is 4 instead of 6. I input month and year to find the no of days
for that date. In this case I input Jan 2013. How can I sql this ?I don't understand how most of the answers provided here not analytically solving the problem with many cases possible.
First let me understand you:
You have 2 dates range and you want to calculate day range for specific month and year between the original date range.
declare @for_month int = 1 --January
declare @for_year int = 2013
declare @StartDate date = '2012-12-20'
declare @EndDate date = '2013-01-04'
SELECT
CASE
WHEN (DATEPART(MONTH, @StartDate) = @for_month and DATEPART(MONTH, @EndDate) = @for_month) and ((DATEPART(YEAR, @StartDate) = @for_year or DATEPART(YEAR, @EndDate) = @for_year)) THEN
DATEDIFF(DAY, @StartDate,@EndDate)
WHEN (@StartDate < cast(CONVERT(varchar(4), @for_year) + '-' + CONVERT(varchar(2), @for_month) + '-01' as date)) and (@EndDate between (cast(CONVERT(varchar(4), @for_year) + '-' + CONVERT(varchar(2), @for_month) + '-01' as date)) and (cast(DATEADD(d, -1, DATEADD(m, DATEDIFF(m, 0, cast( CONVERT(varchar(4), @for_year) + '-' + CONVERT(varchar(2), @for_month) + '-01' as date)) + 1, 0)) as date))) THEN
DATEDIFF(DAY, DATEADD(MONTH, DATEDIFF(MONTH, -1, @EndDate)-1, 0),@EndDate)
WHEN (@EndDate > cast(DATEADD(d, -1, DATEADD(m, DATEDIFF(m, 0, cast( CONVERT(varchar(4), @for_year) + '-' + CONVERT(varchar(2), @for_month) + '-01' as date)) + 1, 0)) as date)) and (@StartDate between (cast(CONVERT(varchar(4), @for_year) + '-' + CONVERT(varchar(2), @for_month) + '-01' as date)) and (cast(DATEADD(d, -1, DATEADD(m, DATEDIFF(m, 0, cast( CONVERT(varchar(4), @for_year) + '-' + CONVERT(varchar(2), @for_month) + '-01' as date)) + 1, 0)) as date))) THEN
DATEDIFF(DAY, @StartDate,DATEADD(d, -1, DATEADD(m, DATEDIFF(m, 0, @StartDate) + 1, 0))) + 1
WHEN ((DATEDIFF(DAY, @StartDate, cast(DATEADD(d, -1, DATEADD(m, DATEDIFF(m, 0, cast( CONVERT(varchar(4), @for_year) + '-' + CONVERT(varchar(2), @for_month) + '-01' as date)) + 1, 0)) as date)) >= 0) and (DATEDIFF(DAY, cast(CONVERT(varchar(4), @for_year) + '-' + CONVERT(varchar(2), @for_month) + '-01' as date), @EndDate) >= 0)) THEN
DATEDIFF(DAY, cast( CONVERT(varchar(4), @for_year) + '-' + CONVERT(varchar(2), @for_month) + '-01' as datetime), DATEADD(d, -1, DATEADD(m, DATEDIFF(m, 0, cast( CONVERT(varchar(4), @for_year) + '-' + CONVERT(varchar(2), @for_month) + '-01' as datetime)) + 1, 0))) + 1
ELSE
0
END as [DD]
I don't know how you calculate day range between 01/01/2013 and 04/01/2013
is 4, it is actually is 3 but if that is the case, you can add 1 from the condition. -
Difference between two dates: a complete solution
There have been many posts asking how to get the number of days between two dates. There is no method in java like the VB method dateDiff(), and there is only the method date.getTime() which gives you the number of milliseconds since Jan 1 1970, the Epoch. I have not seen a solution that makes use of the fact that there are 86400000 milliseconds per day, only when trying to get the number of days , i.e. multiples of 24 hours between two millisecond points in time.
So what if date1 was Oct 26th 11.30pm and date2 was Oct 27th 12.30am? If you get the millisecond difference, and divide by 86400000, you do not notice that these two times are on separate days, but only one hour apart.
My solution takes account of the modulo (%) function. If you use the date.getTime() method, and use % 86400000 on this number of milliseconds, it will tell you how far (milliseconds) this date is into this day, after 12am. If you subtract this amount from the total number of milliseconds, then divide by 86400000, this will give you the number of days after the Epoch, so you are able to know which day the millisecond time is in. If you want to know the days between two millisecond times, this will tell you, whereas just dividing by 86400000, will not, and may be misleading.
I think this could be incorporated into the Date class easily. There are some problems though, as not every day has 86400000 milliseconds, if you are talking about days when Daylight Saying Time begins or ends. Doing calculations like that will create errors, unless you know whether each date is in Daylight Saving Time or not. Leap years are OK, unless want to work out the number of years from the number of days by dividing by 365, as calculating numbers of days is only done by looking at a 24 hour time period. However where 'leap seconds' have been added, like once every ten years, there may be small errors. This was not included in anyone elses calculations.
So the calculation would go like this:
public class BetterDate extends Date
public int dayDifference(Date d)
long time = this.getTime() ; // gets milliseconds
long timed = d.getTime() ; // gets milliseconds of other date
int days = (time - (time % 86400000))/86400000 ; // number of days
int daysd = (timed - (timed % 86400000))/86400000 ; // number of days of other date
int difference = daysd - days ; // difference in days between two dates
return difference ;
If you want you can use the value (time % 86400000), and modulo this amount with the number of milliseconds in one hour, then subtract the result from the (time % 86400000) and divide by the millliseconds in one hour to give you the number of hours, but there are JDK functions that you can use to get the hour if needed.
This method is not perfect, but for most cases will get you the difference in days between two dates.
JamesHi James,
I tried out your solution, but it seems to fail on dates without times. I tries the difference between 31/03/2002 and 01/04/2002 and the result is 0. I guess it is because the times are assumed to be 00:00 as no times are mentioned and 31st March I believe is the date when DST change will take place this year. If I set the two times as well to a time much later than midnight for example 0600 then I seem to get the right answer.
Joag
There have been many posts asking how to get the
number of days between two dates. There is no method
in java like the VB method dateDiff(), and there is
only the method date.getTime() which gives you the
number of milliseconds since Jan 1 1970, the Epoch. I
have not seen a solution that makes use of the fact
that there are 86400000 milliseconds per day, only
when trying to get the number of days , i.e. multiples
of 24 hours between two millisecond points in time.
So what if date1 was Oct 26th 11.30pm and date2 was
Oct 27th 12.30am? If you get the millisecond
difference, and divide by 86400000, you do not notice
that these two times are on separate days, but only
one hour apart.
My solution takes account of the modulo (%) function.
If you use the date.getTime() method, and use %
86400000 on this number of milliseconds, it will tell
you how far (milliseconds) this date is into this day,
after 12am. If you subtract this amount from the total
number of milliseconds, then divide by 86400000, this
will give you the number of days after the Epoch, so
you are able to know which day the millisecond time is
in. If you want to know the days between two
millisecond times, this will tell you, whereas just
dividing by 86400000, will not, and may be
misleading.
I think this could be incorporated into the Date class
easily. There are some problems though, as not every
day has 86400000 milliseconds, if you are talking
about days when Daylight Saying Time begins or ends.
Doing calculations like that will create errors,
unless you know whether each date is in Daylight
Saving Time or not. Leap years are OK, unless want to
work out the number of years from the number of days
by dividing by 365, as calculating numbers of days is
only done by looking at a 24 hour time period. However
where 'leap seconds' have been added, like once every
ten years, there may be small errors. This was not
included in anyone elses calculations.
So the calculation would go like this:
public class BetterDate extends Date
public int dayDifference(Date d)
long time = this.getTime() ; // gets milliseconds
long timed = d.getTime() ; // gets milliseconds of
other date
int days = (time - (time % 86400000))/86400000 ; //
number of days
int daysd = (timed - (timed % 86400000))/86400000 ; //
number of days of other date
int difference = daysd - days ; // difference in days
between two dates
return difference ;
If you want you can use the value (time % 86400000),
and modulo this amount with the number of milliseconds
in one hour, then subtract the result from the (time %
86400000) and divide by the millliseconds in one hour
to give you the number of hours, but there are JDK
functions that you can use to get the hour if needed.
This method is not perfect, but for most cases will
get you the difference in days between two dates.
James -
How can I calculate the maximum number of days between two dates in a range of dates?
I have a column of dates spanning the couse of a few months. I would like to know if I can calculate the maximum number of days between each row and display the highest number. I currently have another column that calculates the days betwen the rows and I am currently just looking at the totals and highlighting the highest period.
Is this possible? Any help or suggestions are appreciated.
Thank you,
TrevorThis sounds totally possible, Can you post a screen shot of your table to make responding more focused? If you mean you want to:
A) compute the difference (in days) between two date in the same row, then
B) find the max duration (in days)
Here is my take on this problem:
D2 = C2-B2
select D2 and fill down
F1=MAX(D)
to perform the conditional formatting (to highlight the max duration) select column D, then set up conditional formatting as shown in the 1st image -
Can Anyone tell me how to find out the gap between two dates. Specifically Without counting Saturday and Sunday.
corlettk wrote:
<intercession>
Has anyone written a DateMath class?... it would be handy... the amount (and ugliness) of the code required to do elementary date mathematics with the raw Calendar is appaling.<recess>
Isn't Java 7 supposed to cure all this headache?
</recess> -
Difference between two dates in hours
hi
i want to write the procedure which will give me the difference of two dates in hours.
there are two columns start_dt and end_dt and i want to calculate the difference between them in hours.
morever i want to add functionality like if the start or end date falls on weekend(saturday & sunday) i want to subtract that whole period(which obviously will be in hours) from the total hours elapsed between two dates.
could anyone please help me in this?
regards,
PDnicolas..
thanks for bringing this into my notice..
i used ur one of the reply..
select
to_char(sysdate,'dd-mm-yyyy hh:mi:ss') d1,
to_char(sysdate-45.4,'dd-mm-yyyy hh:mi:ss') d2,
trunc(sysdate-(sysdate-45.4)) "Days",
trunc(((sysdate-(sysdate-45.4))-trunc(sysdate-(sysdate-45.4)))*24) "Hours",
trunc(((((sysdate-(sysdate-45.4))-trunc(sysdate-(sysdate-45.4)))*24)-trunc(((sysdate-(sysdate-45.4))-trunc(sysdate-(sysdate
-45.4)))*24))*60) "Minutes",
trunc((((((sysdate-(sysdate-45.4))-trunc(sysdate-(sysdate-45.4)))*24)-trunc(((sysdate-(sysdate-45.4))-trunc(sysdate-(sysdat
e-45.4)))*24))*60)-trunc(((((sysdate-(sysdate-45.4))-trunc(sysdate-(sysdate-45.4)))*24)-trunc(((sysdate-(sysdate-45.4))-tru
nc(sysdate-(sysdate-45.4)))*24))*60)) "Second"
from dual
it's working but the problem is that i also have to give functionality like if the start_dt or end_dt is on holiday(i.e. weekend or holiday) i need to subtract the hours spent during holiday period from total hours.
any help?
regards,
PD!! -
How to calculate the hour difference between two dates?
hi all,
how to calculate the hour difference between two dates?
eg i trying this...
((TO_DATE(TO_CHAR(GRNi.reference_date_4,'hh24:mi'),'hh24:mi') -
TO_DATE(TO_CHAR(NVL(GRNi.reference_date_3,SYSDATE),'hh24:mi'),'hh24:mi'))*24)*60 Act_Hr
Reg.
AAKHi
To break the diff between 2 dates into days, hours, minutes, sec -- you can use the following:
select to_char( created, 'dd-mon-yyyy hh24:mi:ss' ),
trunc( sysdate-created ) "Dy",
trunc( mod( (sysdate-created)*24, 24 ) ) "Hr",
trunc( mod( (sysdate-created)*24*60, 60 ) ) "Mi",
trunc( mod( (sysdate-created)*24*60*60, 60 ) ) "Sec",
to_char( sysdate, 'dd-mon-yyyy hh24:mi:ss' ),
sysdate-created "Tdy",
(sysdate-created)*24 "Thr",
(sysdate-created)*24*60 "Tmi",
(sysdate-created)*24*60*60 "Tsec"
from all_users
where rownum < 50
HTH
RangaReddy -
To find difference between two dates
Hi all,
I am new to this forum and oracle.
I want to get the difference between two dates. My query is as below...
sqlserver_utilities.datediff('YY', startdate,enddate)
I want the difference in year.
Please help me. It's really urgent.
Thanks in advance.
Regards,
InamSelect to_char(enddate,'YY') - to_char(startdate,'YY') fromPLEASE don't do that. There are so many things wrong with it...
for example:
1). Why are you subtracting character data types?
2). What if the start date is 1999 and the end date is 2000? Do you expect to get a difference of -1?
3). What if the start date is 1 Jan 2000 and the end date is 31 Dec 2000? Do you expect to get 0 instead of 1 or .997?
4). Why would you convert dates to something else when they are inherently subtractable.
5). There are obvious points in the OP's "specification" that are vague - the best thing (after telling them to search, of course since this has been answered a million times already) would be to try to clarify the spec.
John
Maybe you are looking for
-
I am fairly new to ID, been a Quarker for gazillion years. May I ask a question? I am working on a clients art in InDesign some of the text has gold squares behind it (not always the entire word) what does this mean I know pink means font is missing
-
Set Not to Sleep, But it Does Anyway
Although I have my system set to " Never" sleep it still does and I don't want it to. When I go to bed at night I leave my system running, when I wake up it's sleeping and my AirPort is off. Any clues why? BTW, display is set to sleep after 20 min. T
-
Decode with multiple column sub query
Dear's What is the wrong with the query select * from common_master where (company_code,lpad(rtrim(ltrim(bu_code)),12,' ')) IN decode ('ADMIN','ADMIN',(select company_code ,lpad(rtrim(ltrim(bu_code)),12,' ') from common_master ) ,(select company_code
-
I would like to look through my drive for locked files. I cannot find a way to search for them either with the Finder or with a terminal application. Does someone know how I might do this? Thank you
-
1GB MMC in 6230, only recognises so many tracks.
I have a 6230, with a 1GB MMC. it can store 500+ tracks. but when i open Music Player it only recognises 200 of the tracks, which means i have to keep changing the 200 all the time. is there a way for the phone to see all of the tracks on the card?