Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select rows within some date range

Tags:

mysql

I want to select rows that falls between some date range, I tried the following query but it didn't work.

SELECT * FROM tbl WHERE DATE_FORMAT(date_col, '%2011-%c-%e') BETWEEN '2011-11-28' AND '2011-12-5' 

It doesn't seem like the BETWEEN keyword works on date. Please how do I get the results? Thanks

like image 659
Chibuzo Avatar asked Jun 21 '26 18:06

Chibuzo


1 Answers

You don't need to use DATE_FORMAT if you want to compare dates.

SELECT * 
FROM tbl 
WHERE DATE(date_col) BETWEEN '2011-11-28' AND '2011-12-05'

Your code compares strings, assuming you use DATE_FORMAT(date_col, '%Y-%c-%e')

like image 192
a1ex07 Avatar answered Jun 24 '26 08:06

a1ex07