Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get current year in MySQL WHERE clause

Tags:

sql

mysql

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.

like image 377
sauce Avatar asked Feb 26 '17 11:02

sauce


People also ask

How do I get this year data in MySQL?

Use the YEAR() function to retrieve the year value from a date/datetime/timestamp column in MySQL. This function takes only one argument – a date or date and time. This can be the name of a date/datetime/timestamp column or an expression returning one of those data types.

How do I get the current year in SQL query?

Just run these SQL queries one by one to get the specific element of your current date/time: Current year: SELECT date_part('year', (SELECT current_timestamp));

What is Getdate () in MySQL?

The GETDATE() function returns the current database system date and time, in a 'YYYY-MM-DD hh:mm:ss. mmm' format.


1 Answers

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());
like image 186
Gurwinder Singh Avatar answered Nov 07 '22 16:11

Gurwinder Singh