Search code examples
sqloracle-databaseselectdatediff

DATEDIFF function in Oracle


I need to use Oracle but DATEDIFF function doesn't work in Oracle DB.

How to write the following code in Oracle? I saw some examples using INTERVAL or TRUNC.

SELECT DATEDIFF ('2000-01-01','2000-01-02') AS DateDiff;

Solution

  • In Oracle, you can simply subtract two dates and get the difference in days. Also note that unlike SQL Server or MySQL, in Oracle you cannot perform a select statement without a from clause. One way around this is to use the builtin dummy table, dual:

    SELECT TO_DATE('2000-01-02', 'YYYY-MM-DD') -  
           TO_DATE('2000-01-01', 'YYYY-MM-DD') AS DateDiff
    FROM   dual