MySQL Forums
Forum List  »  Newbie

Re: Filter by Year(date)
Posted by: Barry Galbraith
Date: November 23, 2022 09:54PM

Do you mean "last 2 years" or where YEAR is the last 2 year numbers, which could be as little as 1 year and 1 day, depending on when you look.

This will get you the data with a YEAR 2020 or 2021, or 2022.
SELECT ...
WHERE YEAR(data) >= YEAR(current_date()) - 2;

If you want the last two years then you need this
SELECT ... WHERE
data between DATE_SUB(NOW(), INTERVAL 2 YEAR) and NOW();

NOW() is the same as current_date()

Good luck,
Barry.

Options: ReplyQuote


Subject
Written By
Posted
November 23, 2022 12:50PM
November 23, 2022 12:57PM
Re: Filter by Year(date)
November 23, 2022 09:54PM
November 24, 2022 03:16AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.