I need to subtract two dates. First I convert the date field to years using to_char, then convert to a number using to_number. However I get an error in the query when subtracting two years:
to_char((order date),'yyyy')
to_char((beginning of the year),'yyyy')
I tried converting to a number using
to_number(to_char((order date),'yyyy'),'9999')
to_number(to_char((beginning of the year),'yyyy'),'9999')
>Solution :
Use extract; it is simpler than what you’re doing:
select extract(year from order_date) - extract(year from trunc(sysdate, 'yyyy')) as result
from some_table