'How to calculate difference between two dates in oracle 11g SQL
When I am trying to calculate the date difference by using datediff function it showing that invalid identifier.
SELECT DATEDIFF(day,'2008-08-05','2008-06-05') AS DiffDate from da_static_trade.
Error : invalid identifier.
Can you please tell me what is the function to calculate date difference.
Solution 1:[1]
There is no DATEDIFF()
function in Oracle. On Oracle, it is an arithmetic issue
select DATE1-DATE2 from table
Solution 2:[2]
Oracle support Mathematical Subtract -
operator on Data datatype. You may directly put in select clause following statement:
to_char (s.last_upd – s.created, ‘999999D99?)
Check the EXAMPLE for more visibility.
In case you need the output in termes of hours, then the below might help;
Select to_number(substr(numtodsinterval([END_TIME]-[START_TIME]),’day’,2,9))*24 +
to_number(substr(numtodsinterval([END_TIME]-[START_TIME],’day’),12,2))
||':’||to_number(substr(numtodsinterval([END_TIME]-[START_TIME],’day’),15,2))
from [TABLE_NAME];
Solution 3:[3]
You can not use DATEDIFF
but you can use this (if columns are not date type):
SELECT
to_date('2008-08-05','YYYY-MM-DD')-to_date('2008-06-05','YYYY-MM-DD')
AS DiffDate from dual
you can see the sample
Solution 4:[4]
You can use this:
SET FEEDBACK OFF;
SET SERVEROUTPUT ON;
DECLARE
V_START_DATE CHAR(17) := '28/03/16 17:20:00';
V_END_DATE CHAR(17) := '30/03/16 17:50:10';
V_DATE_DIFF VARCHAR2(17);
BEGIN
SELECT
(TO_NUMBER( SUBSTR(NUMTODSINTERVAL(TO_DATE(V_END_DATE , 'DD/MM/YY HH24:MI:SS') - TO_DATE(V_START_DATE, 'DD/MM/YY HH24:MI:SS'), 'DAY'), 02, 9)) * 24) +
(TO_NUMBER( SUBSTR(NUMTODSINTERVAL(TO_DATE(V_END_DATE , 'DD/MM/YY HH24:MI:SS') - TO_DATE(V_START_DATE, 'DD/MM/YY HH24:MI:SS'), 'DAY'), 12, 2))) ||
SUBSTR(NUMTODSINTERVAL(TO_DATE(V_END_DATE , 'DD/MM/YY HH24:MI:SS') - TO_DATE(V_START_DATE, 'DD/MM/YY HH24:MI:SS'), 'DAY'), 14, 6) AS "HH24:MI:SS"
INTO V_DATE_DIFF
FROM
DUAL;
DBMS_OUTPUT.PUT_LINE(V_DATE_DIFF);
END;
Solution 5:[5]
basically the to_char(sysdate,'DDD') returns no of days from 1-jan-yyyy to sysdate so that if subtract two dates it will return that,you will get difference between two dates
select to_char(sysdate,'DDD') -to_char(to_date('19-08-1995','dd-mm-yyyy'),'DDD') from dual;
Solution 6:[6]
Use this in select statement:
(((cast(<column_end_date> as date) - cast(<column_start_date> as date)) * 24 * 60)/60) as "SECONDS"
This should work for you.
Solution 7:[7]
Oracle DateDiff is from a different product, probably mysql (which is now owned by Oracle).
The difference between two dates (in oracle's usual database product) is in days (which can have fractional parts). Factor by 24 to get hours, 24*60 to get minutes, 24*60*60 to get seconds (that's as small as dates go). The math is 100% accurate for dates within a couple of hundred years or so. E.g. to get the date one second before midnight of today, you could say
select trunc(sysdate) - 1/24/60/60 from dual;
That means "the time right now", truncated to be just the date (i.e. the midnight that occurred this morning). Then it subtracts a number which is the fraction of 1 day that measures one second. That gives you the date from the previous day with the time component of 23:59:59.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Mikhail Timofeev |
Solution 2 | Nadeem_MK |
Solution 3 | Ersin Gülbahar |
Solution 4 | |
Solution 5 | sandeep veeramalla |
Solution 6 | user2648264 |
Solution 7 | Malcolm |