Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a date is between two dates mysql?

I'm querying dates from a mysql database. When I try to select only the dates between a certain from and to date, I'm not getting the expected results back.

My query:

select * from tbl_billing where date BETWEEN '09-02-2017' and '10-02-2017'

My output:

09-02-2017
10-02-2017
10-01-2017
09-01-2017
09-01-2017

My desired result:

09-02-2017
10-02-2017
like image 778
phpguru Avatar asked Oct 16 '25 04:10

phpguru


1 Answers

You can use canonical sql date format

select * from tbl_billing where date BETWEEN '2017-02-09' and '2017-02-10'

or convert properly using str_to_date

select * from tbl_billing where date BETWEEN str_to_date('09-02-2017', '%d-%m-%Y')
            and  str_to_date('10-02-2017', '%d-%m-%Y')

but if also your date is a varchar then you must convert this column too

select * from tbl_billing where str_to_date(date,'%d-%m-%Y')  BETWEEN str_to_date('09-02-2017', '%d-%m-%Y')
            and  str_to_date('10-02-2017', '%d-%m-%Y')
like image 164
ScaisEdge Avatar answered Oct 18 '25 05:10

ScaisEdge



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!