Im looking for the easiest way to select/return the last month/day of the current year in the format of 2016-12-31 using MySQL. I've seen examples using Postgres and SQL Server but not MySQL.
Here's one solution not very elegant.
SELECT DATE_FORMAT(NOW(),'%Y-12-31')
There might be easier solutions out there, but here's an approach to this using LAST_DAY()
and DATE_ADD()
:
SELECT LAST_DAY(DATE_ADD(NOW(), INTERVAL 12-MONTH(NOW()) MONTH));
This should return the last day of the current year.
Try to use this one:
SELECT DATE(CONCAT(YEAR(CURDATE()),"-12-31"))
There is no need to find the last day of the year by add or sub days. You already know that is 31th of th december every year. So just give to Mysql the unique real variable: the current year.
This return a DATE not a date formatted STRING.
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