Alex Rivera | Logout

Querying last 5 years

Asked 2012-02-08T16:02:59.603
16

I want to query all products sold in the last 5 years.
It is possible to do it like this:

select * from products
where time between sysdate-1826 and sysdate

But it there also a nicer way instead of calculating all the days and subtract it from sysdate?

Edit
Report

1 Answer

29
SELECT *
  FROM products
 WHERE date_column >= add_months( sysdate, -12*5 )

or

SELECT *
  FROM products
 WHERE date_column >= sysdate - interval '5' year

will both give you all the rows from the last 5 years (though you would generally want to add a TRUNC to remove the time portion unless you really care whether a row was created on Feb 8, 2007 in the morning or in the afternoon).

answered 2012-02-08T16:08:14.010

Your Answer