Subtracting dates [message #649568] |
Thu, 31 March 2016 03:39 |
|
javon13
Messages: 17 Registered: October 2015
|
Junior Member |
|
|
Hello everyone,
I am in need to subtract two dates and get a number of days.
OK, that's easy enough. But I realized this:
BEGIN
DBMS_OUTPUT.put_line (to_date('2010-10-05','YYYY-MM-DD') - SYSDATE);
END;
/
This gives me the result:
-2004,433043981481481481481481481481481481
As I need exact number of days I'd like to get rid of numbers behind decimal point.
And this is the question for the forum: Is it safe enough to use it like this?:
BEGIN
DBMS_OUTPUT.put_line (to_date('2010-10-05','YYYY-MM-DD') - to_date(SYSDATE));
END;
/
And the result is:
-2004
In real life I have two dates (DD.MM.YY) from the table. Something like:
declare
dateone DATE;
datetwo DATE;
rs_days NUMBER;
begin
dateone := PKG.GETPARAM('START_DATE');
datetwo := ADD_MONTHS(dateone,4);
rs_days := to_date(dateone) - to_date(datetwo);
dbms_output.put_line('Days: ' || rs_days);
end;
/
Thanks again in advance for your time.
[Updated on: Thu, 31 March 2016 03:40] Report message to a moderator
|
|
|
Re: Subtracting dates [message #649570 is a reply to message #649568] |
Thu, 31 March 2016 03:51 |
John Watson
Messages: 8929 Registered: January 2010 Location: Global Village
|
Senior Member |
|
|
This is a bug waiting to bite, DBMS_OUTPUT.put_line (to_date('2010-10-05','YYYY-MM-DD') - to_date(SYSDATE)); if you want to strip off the hours/minutes/seconds, use the TRUNC function.
|
|
|
|
|
Re: Subtracting dates [message #649573 is a reply to message #649571] |
Thu, 31 March 2016 04:02 |
John Watson
Messages: 8929 Registered: January 2010 Location: Global Village
|
Senior Member |
|
|
orclz> select sysdate,trunc(sysdate) from dual;
SYSDATE TRUNC(SYSDATE)
------------------- -------------------
2016-03-31:10:02:09 2016-03-31:00:00:00
orclz>
|
|
|
|
|
|
|
Re: Subtracting dates [message #649649 is a reply to message #649633] |
Fri, 01 April 2016 14:26 |
|
Barbara Boehmer
Messages: 9088 Registered: November 2002 Location: California, USA
|
Senior Member |
|
|
Bill,
I don't have his packaged function code and don't know what it does, presumably something more complex than just returning sysdate. I provided a packaged function that just returns sysdate, in order to have a packaged function to call to demonstrate code similar to what he said he has. Obviously, if it just returns sysdate, then your code would be simpler.
Barbara
|
|
|