If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > Oracle > DATEDIFF function in Oracle

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old
Registered User
 
Join Date: Oct 2006
Posts: 83
DATEDIFF function in Oracle

Hi ,

I am having a SQL Server query with DATEDIFF function in it .

PHP Code:
SELECT DATEDIFF(ddmydate GETDATE()) 
This query will extract the difference between the days of mydate and GETDATE() values .
Refer below link for DATEDIFF function on SQL Server



I tried to convert the query to Oracle and execute it .
But there is no DATEDIFF function in Oracle .
I tried using ROUND function like

Quote:
SELECT ROUND(( (SYSDATE) - mydate ))
But of no use .

Kindly suggest me the equivalent for the SQL Server DATEDIFF function in Oracle

Thanks
Reply With Quote
  #2 (permalink)  
Old
Registered User
 
Join Date: Mar 2002
Location: Reading, UK
Posts: 1,133
The nice thing about Oracle Dates is that they are held as real numbers where 1 represents a day. So your minus should work, did you forget to select from dual?

So difference in days is
select SYSDATE-to_date('01-jan-2007','dd-mon-yyyy') from dual

returns 165.35.... Use round() to round to the nearest day or trunc() to round down to the number of whole days.

If you want it in hours multiply the result by 24, minutes then 1440, seconds then 86400.

Alan
Reply With Quote
  #3 (permalink)  
Old
Registered User
 
Join Date: Oct 2006
Posts: 83
Lightbulb

The above Query will return an intervel .

I tried executing the below query where i have the datediff function in a where condition .

Quote:
select tem1 from tabtemp where ROUND((SYSDATE) - mydate) = 0;
The Oracle throws the following error

ORA-00932: inconsistent datatypes: expected NUMBER got INTERVAL

Kindly suggest me
Reply With Quote
  #4 (permalink)  
Old
Registered User
 
Join Date: Mar 2002
Location: Reading, UK
Posts: 1,133
OK, if your using a timestamp field instead of a date field then you can do the following

where (SYSTIMESTAMP - mydate)<numtodsinterval(1,'day');

however this is probably better in terms of performance

where mydate>(systimestamp-1);

Alan
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On