0

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!

Vega
  • 2,661
  • 5
  • 24
  • 49

1 Answers1

1

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
GriGrim
  • 2,891
  • 1
  • 19
  • 33
  • Forgot that I use YEAR(t.date) >=, edited my code. DESCRIBE shows `SQL_TYPE DATE` for column "date" – Vega Jan 05 '21 at 11:04