Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the number of days between 2 dates in Oracle 11g?

I'm trying to find an integer number of days between two dates in Oracle 11g.

I can get close by doing

select sysdate - to_date('2009-10-01', 'yyyy-mm-dd') from dual

but this returns an interval, and I haven't been successful casting this to an integer.

Edit: Apparently in 10g, this returns the number of days as an integer.

like image 735
Brian Ramsay Avatar asked Oct 29 '09 19:10

Brian Ramsay


People also ask

How can I find the difference between two dates in Oracle 11g?

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.

How do I find the number of days between two dates in PL SQL?

Just subtract the two date values. For example: SELECT SYSDATE - TO_DATE('03-02-1996','MM-DD-YYYY') FROM dual; The result is fractional in days.

How do I find the difference between two dates and time in Oracle?

Discussion: To calculate the difference between the timestamps in Oracle, simply subtract the start timestamp from the end timestamp (here: arrival - departure ). The resulting column will be in INTERVAL DAY TO SECOND . The first number you see is the number of whole days that passed from departure to arrival .

Does Oracle have a datediff function?

Use the @DATEDIFF function to calculate the difference between two dates or datetimes, in days or seconds. The difference between the specified dates. Valid values can be: DD , which computes the difference in days.


5 Answers

Or you could have done this:

select trunc(sysdate) - to_date('2009-10-01', 'yyyy-mm-dd') from dual

This returns a NUMBER of whole days:

SQL> create view v as 
  2  select trunc(sysdate) - to_date('2009-10-01', 'yyyy-mm-dd') diff 
  3  from dual;

View created.

SQL> select * from v;

      DIFF
----------
        29

SQL> desc v
 Name                   Null?    Type
 ---------------------- -------- ------------------------
 DIFF                            NUMBER(38)
like image 99
Tony Andrews Avatar answered Oct 06 '22 05:10

Tony Andrews


I figured it out myself. I need

select extract(day from sysdate - to_date('2009-10-01', 'yyyy-mm-dd')) from dual
like image 21
Brian Ramsay Avatar answered Oct 06 '22 05:10

Brian Ramsay


You can try using:

select trunc(sysdate - to_date('2009-10-01', 'yyyy-mm-dd')) as days from dual
like image 32
Sabeen Malik Avatar answered Oct 06 '22 06:10

Sabeen Malik


This will work i have tested myself.
It gives difference between sysdate and date fetched from column admitdate

  TABLE SCHEMA:
    CREATE TABLE "ADMIN"."DUESTESTING" 
    (   
  "TOTAL" NUMBER(*,0), 
"DUES" NUMBER(*,0), 
"ADMITDATE" TIMESTAMP (6), 
"DISCHARGEDATE" TIMESTAMP (6)
    )

EXAMPLE:
select TO_NUMBER(trunc(sysdate) - to_date(to_char(admitdate, 'yyyy-mm-dd'),'yyyy-mm-dd')) from admin.duestesting where total=300
like image 22
Akki Avatar answered Oct 06 '22 04:10

Akki


  • Full days between end of month and start of today, including the last day of the month:

    SELECT LAST_DAY (TRUNC(SysDate)) - TRUNC(SysDate) + 1 FROM dual
    
  • Days between using exact time:

    SELECT SysDate - TO_DATE('2018-01-01','YYYY-MM-DD') FROM dual
    
like image 28
Oleksiy Avatar answered Oct 06 '22 05:10

Oleksiy