sauce
sauce

Reputation: 345

Get current year in MySQL WHERE clause

How do you get the current year (as a four digit number) for use in a MySql WHERE clause?

I have the following query:

SELECT * FROM tblfeatured WHERE yeargrad LIKE date('Y')

The yeargrad columns stores just a year (e.g 2017) and I would like to find rows where this matches the current year.

Upvotes: 19

Views: 42234

Answers (3)

You Old Fool
You Old Fool

Reputation: 22941

For completeness sake, please note you can also use DATE_FORMAT()

DATE_FORMAT(NOW(), '%Y')

or

DATE_FORMAT(CURDATE(), '%Y')

Upvotes: 3

Rubin Anbin
Rubin Anbin

Reputation: 328

SELECT * FROM tblfeatured WHERE YEAR(yeargrad) = YEAR(CURRENT_DATE())

Upvotes: 5

Gurwinder Singh
Gurwinder Singh

Reputation: 39497

You can use curdate() function to get today's date and then use year() function on it to get the current year.

SELECT * FROM tblfeatured WHERE yeargrad = year(curdate());

Upvotes: 29

Related Questions