Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is it safe to use 31 for "last day of the month" when comparing dates in MySQL

Tags:

mysql

I know this is not the perfect approach but is there a problem when comparing dates in MySQL and other DBs to just use Y-m-31 as the last day of the month?

For example

SELECT * FROM orders WHERE date > 2015-02-01 AND date < 2015-02-31

like image 613
d.raev Avatar asked Dec 21 '25 20:12

d.raev


1 Answers

You can use the MySQL LAST_DAY() function for this:

SELECT * FROM orders WHERE date >= '2015-02-01' AND date <= LAST_DAY('2015-02-01')

(you probably also want >= and <= instead of excluding the first/last days of each month)

like image 56
ceejayoz Avatar answered Dec 23 '25 13:12

ceejayoz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!