Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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; 
like image 340
user3601310 Avatar asked Feb 09 '15 09:02

user3601310


People also ask

Can I use datediff in Oracle?

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.

What is a datediff () function?

You can use the DateDiff function to determine how many specified time intervals exist between two dates. For example, you might use DateDiff to calculate the number of days between two dates, or the number of weeks between today and the end of the year.

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

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 .

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.


1 Answers

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 
like image 79
Mureinik Avatar answered Oct 25 '22 12:10

Mureinik