How can I filter for CURRENT_DATE / SYSDATE - 2 years?

467 Views Asked by At

I have a table t with a column "date" which has the type "DATE":

date
2018-10-01
2019-02-03
2020-01-01
2021-01-01

I want to get only entries where CURRENT_DATE / SYSDATE minus 2 years is true. So the result should be (CURRENT_DATE / SYSDATE = "2021-05-01":

date
2019-02-03
2020-01-01
2021-01-01

My code:

SELECT *
FROM t
WHERE YEAR(t.date) >= ADD_YEARS(TRUNC(CURRENT_DATE), -2)

But that gives me the error

Feature not supported: Incomparable Types: DECIMAL(4,0) and DATE!

Using SYSDATE with

SELECT *
FROM t
WHERE YEAR(t.date) >= ADD_YEARS(TRUNC(SYSDATE), -2)

gives the error

Feature not supported: Incomparable Types: DECIMAL(4,0) and DATE!

I tried https://stackoverflow.com/a/28888880/4435175 with

SELECT *
FROM t
WHERE YEAR(t.date) >= add_months( trunc(sysdate), -12*2 )

but that gave me the same error

Feature not supported: Incomparable Types: DECIMAL(4,0) and DATE!

1

There are 1 best solutions below

1
GriGrim On

sysdate already returns you date. No need to trunc it.

Looks like something is wrong with data type of column t.date.

What describe t; shows you?

UPD.

I see 2 options here. First one is more preferable for me, as it doesn't apply function to every t.date value.

SELECT *
FROM t
WHERE t.date >= add_years (sysdate, -2)
--WHERE years_between (sysdate, t.date) >= 2