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.
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.
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));
The GETDATE() function returns the current database system date and time, in a 'YYYY-MM-DD hh:mm:ss. mmm' format.
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());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With