Find if a date is in date range using MySQL -
hi have following values stored in mysql table:
--- ------------------------ ------------------- id | startdate (varchar (20)) | enddate(varchar(20)) --- ------------------------ ------------------- 1 | 03-04-2017 | 18-04-2017
i using following sql find if date within startdate , enddate:
select (date_format(str_to_date('03-04-2017','%d-%m-%y'),'%d-%m-%y') >= startdate , date_format(str_to_date('03-04-2017','%d-%m-%y'),'%d-%m-%y') <= enddate) valid holiday
my issue when execute query , provide 03-04-2017 returns 1 returns 1 when provide 03-05-2017.
could please highlight wrong in query?
use query this:
select * holiday str_to_date(startdate,'%d-%m-%y') between str_to_date('03-04-2017','%d-%m-%y') , str_to_date('03-04-2017','%d-%m-%y');
sample
mysql> select if(str_to_date('11-04-2017','%d-%m-%y') -> between -> str_to_date('03-04-2017','%d-%m-%y') -> , -> str_to_date('10-04-2017','%d-%m-%y') -> -> ,'yes','no') answer; +--------+ | answer | +--------+ | no | +--------+ 1 row in set (0,00 sec) mysql> select if(str_to_date('04-04-2017','%d-%m-%y') -> between -> str_to_date('03-04-2017','%d-%m-%y') -> , -> str_to_date('10-04-2017','%d-%m-%y') -> -> ,'yes','no') answer; +--------+ | answer | +--------+ | yes | +--------+ 1 row in set (0,01 sec) mysql>
Comments
Post a Comment