1

I was trying to find the no of days between 2 dates. I tried running the below query and ended up with ORA-30076 error.

Query is,

select extract(day from (sysdate - to_date('2009-10-01', 'yyyy-mm-dd'))) 
 from dual

Error is,

Error at Command Line:1 Column:34
Error report:
SQL Error: ORA-30076: invalid extract field for extract source
30076. 00000 -  "invalid extract field for extract source"
*Cause:    The extract source does not contain the specified extract field.

But when i try to run

Select extract(day from sysdate) from dual;

i got correct output.

Please help if anyone knows the reason as why i am getting the above error.

Nick Krasnov
  • 26,886
  • 6
  • 61
  • 78
prabu
  • 1,247
  • 7
  • 21
  • 33

3 Answers3

8

Subtracting one date from another doesn't return a date, it returns a number that represents the number of days difference so you can't use extract.

DoctorMick
  • 6,703
  • 28
  • 26
3

sysdate - to_date('2009-10-01', 'yyyy-mm-dd') will give the difference in days between two dates - there is no need to use extract function. Moreover in this situation it would be incorrect usage of extract function simply because it would expect expression of date or interval datatype after from clause and result of sysdate - to_date('2009-10-01', 'yyyy-mm-dd') subtraction is of number datatype - cause of the error you are facing.

Nick Krasnov
  • 26,886
  • 6
  • 61
  • 78
2

This:

 sysdate - to_date('2009-10-01', 'yyyy-mm-dd')

returns a number, not a date. You can only extract days from dates.

Dan Bracuk
  • 20,699
  • 4
  • 26
  • 43