Home > Database > Mysql Tutorial > How to Accurately Calculate the Number of Days Between Two Dates in Oracle 11g?

How to Accurately Calculate the Number of Days Between Two Dates in Oracle 11g?

DDD
Release: 2024-12-21 19:24:21
Original
616 people have browsed it

How to Accurately Calculate the Number of Days Between Two Dates in Oracle 11g?

Determining the Number of Days Between Dates in Oracle 11g

In Oracle 11g, calculating the number of days between two dates requires a straightforward approach. Despite attempting to convert an interval to an integer, the results are not as expected.

Fortunately, an alternative solution exists that yields the desired integer value. By using the trunc function to round down the start date to the nearest whole day, you can subtract it from the end date:

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

This approach returns a NUMBER data type representing the integer number of days between the specified dates. For example:

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)
Copy after login

The above is the detailed content of How to Accurately Calculate the Number of Days Between Two Dates in Oracle 11g?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template