Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

finding difference in days between two dates

user10636796Dec 18 2011 — edited Dec 19 2011
Hi, Everyone,

I am trying to find out the difference in days between two dates and the query i am passing is

SELECT TO_char(sysdate, 'dd/mm/yyyy')-TO_char('15/11/2011', 'dd/mm/yyyy') DAYS FROM DUAL

the error i am getting is

ORA-01722: invalid number
01722. 00000 - "invalid number"
*Cause:
*Action:

Could anybody please help.

Thanks in advance
This post has been answered by sb92075 on Dec 18 2011
Jump to Answer

Comments

895612
Hi,

Can you try this.

SELECT trunc(sysdate)-TO_date('15/11/2011', 'dd/mm/yyyy') DAYS FROM DUAL
sb92075
user10636796 wrote:
Hi, Everyone,

I am trying to find out the difference in days between two dates and the query i am passing is

SELECT TO_char(sysdate, 'dd/mm/yyyy')-TO_char('15/11/2011', 'dd/mm/yyyy') DAYS FROM DUAL
you do NOT subtract strings from each other.
you subtract DATE datatypes

SELECT sysdate - TO_DATE('15/11/2011', 'dd/mm/yyyy') DAYS FROM DUAL
SELECT sysdate - TO_DATE('15/11/2011', 'dd/mm/yyyy') DAYS FROM DUAL;

      DAYS
----------
 33.797419
Edited by: sb92075 on Dec 18, 2011 7:08 PM
User_Z52BC
 
SELECT sysdate - TO_date('15/11/2011', 'dd/mm/yyyy') DAYS FROM DUAL
/
      DAYS
----------
33.9193519

OR

SELECT trunc(sysdate) - TO_date('15/11/2011', 'dd/mm/yyyy') DAYS FROM DUAL
/

      DAYS
----------
        33
fame
Hi,
I think query is useful for you

select to_date(sysdate,'dd-mon-yy')-to_date('15-nov-11','dd-mon-yy') from dual;

Regards,
Fame
523861
fame wrote:
Hi,
I think query is useful for you

select to_date(sysdate,'dd-mon-yy')-to_date('15-nov-11','dd-mon-yy') from dual;

Regards,
Fame
no, this is as useless as the original post's query.

First, you implicitly convert sysdate to a string using your NLS_DATE_FORMAT parameter, then you convert it back using the to_date function and hope that the format mask matches your nls_date format.

treat dates as dates.
sysdate - to_date('15-nov-11','dd-mon-yy')
user10636796
Hi, everyone,

Thanks a lot for all the replies. I am trying to implement this into a statement to my table like this


SELECT trunc(sysdate) - TO_char(date_last_recommended, 'dd/mm/yyyy') DAYS FROM recommendation;

the datatype for date_last_recommended in the table is date.

I am getting this error again

ORA-01722: invalid number
01722. 00000 - "invalid number"
*Cause:
*Action:

The oracle database version i am using is 11.1..0.6.

Could you please help.

Thanks a lot in advance
sb92075
Answer
user10636796 wrote:
Hi, everyone,

Thanks a lot for all the replies. I am trying to implement this into a statement to my table like this


SELECT trunc(sysdate) - TO_char(date_last_recommended, 'dd/mm/yyyy') DAYS FROM recommendation;
SELECT trunc(sysdate) - TRUNC(date_last_recommended) DAYS FROM recommendation;
Marked as Answer by user10636796 · Sep 27 2020
user10636796
Thanks once again everyone.
B Dave
user10636796 wrote:
Hi, Everyone,

I am trying to find out the difference in days between two dates and the query i am passing is

SELECT TO_char(sysdate, 'dd/mm/yyyy')-TO_char('15/11/2011', 'dd/mm/yyyy') DAYS FROM DUAL

the error i am getting is

ORA-01722: invalid number
01722. 00000 - "invalid number"
*Cause:
*Action:

Could anybody please help.

Thanks in advance
use this.

SELECT sysdate-TO_date('15/12/2011', 'dd/mm/yyyy') DAYS FROM DUAL;


By the way,
If you are using difference between two dates in PL. You can use following simple query.
SELECT dt1-dt2 from dual;
where you can use any table instead of dual. As well as any date variable having value in it.

Regards,
Dave.
BluShadow
897450 wrote:
By the way,
If you are using difference between two dates in PL. You can use following simple query.
SELECT dt1-dt2 from dual;
where you can use any table instead of dual. As well as any date variable having value in it.
No, if you're finding the data different in PL, you would simply do...
  v_numvar := dt1-dt2;
Your select statement won't work in PL, because you are not selecting INTO any variable.

Yes, you can do the data calculation in SQL within PL, but that is only valid if you are doing the SQL for a good reason (i.e. as part of fetching some data from tables), otherwise you are context switching from the PL engine to the SQL engine for no good reason, when PL is perfectly capable of doing the calculation itself.
1 - 10
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Jan 16 2012
Added on Dec 18 2011
10 comments
256,842 views