Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to calculate difference between two dates in oracle 11g SQL

Tags:

sql

oracle11g

When I am trying to calculate the date difference by using datediff function it showing that invalid identifier.

SELECT DATEDIFF(day,'2008-08-05','2008-06-05') AS DiffDate from da_static_trade.

Error : invalid identifier.

Can you please tell me what is the function to calculate date difference.

like image 947
Santhosh Kancherla Avatar asked Sep 02 '14 09:09

Santhosh Kancherla


People also ask

How can I find the difference between two date fields 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 .

How do I get the difference between two dates in SQL?

The DATEDIFF() function returns the difference between two dates.

Can you compare dates in Oracle?

You can use "classic comparators" (like >, >=, <, =...) to compare 2 dates. If you have date data types : don't convert them. if you have no choice, use something like: if to_char(d1,'YYYYMMDD') > to_char(d2,'YYYYMMDD') then ... else ...

How do I calculate 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.


2 Answers

There is no DATEDIFF() function in Oracle. On Oracle, it is an arithmetic issue

select DATE1-DATE2 from table 
like image 168
Mikhail Timofeev Avatar answered Oct 13 '22 14:10

Mikhail Timofeev


You can not use DATEDIFF but you can use this (if columns are not date type):

SELECT 
to_date('2008-08-05','YYYY-MM-DD')-to_date('2008-06-05','YYYY-MM-DD') 
AS DiffDate from dual

you can see the sample

http://sqlfiddle.com/#!4/d41d8/34609

like image 41
Ersin Gülbahar Avatar answered Oct 13 '22 14:10

Ersin Gülbahar