DATEDIFF function in Oracle

user3601310 picture user3601310 · Feb 9, 2015 · Viewed 371.2k times · Source

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;

Answer

Mureinik picture Mureinik · Feb 9, 2015

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