Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL Select last day of current year

Tags:

mysql

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.

like image 572
Tom Shaw Avatar asked Jan 25 '16 18:01

Tom Shaw


Video Answer


3 Answers

Here's one solution not very elegant.

SELECT DATE_FORMAT(NOW(),'%Y-12-31')
like image 86
Tom Shaw Avatar answered Oct 10 '22 01:10

Tom Shaw


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.

like image 36
Bjoern Avatar answered Oct 09 '22 23:10

Bjoern


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.

like image 3
Max Cuttins Avatar answered Oct 09 '22 23:10

Max Cuttins