MySQL convert between two date format

2020-03-31 06:30发布

问题:

The user will enter the date in this format : 17-Feb-2017

and the date stored in mysql db is in this format : 2015-02-17 00:00:00

what I tried to do is

SELECT * FROM insurance where DATE_FORMAT(in_date,'%d-%b-%Y') > '17-Feb-2017';

DATE_FORMAT(in_date,'%d-%b-%Y') return the date in this format : 17-Feb-2017

but that not working !!

any suggestions ?

回答1:

Convert the input to a date type and the column value too:

SELECT * FROM insurance where date(in_date) > STR_TO_DATE('17-Feb-2017','%d-%b-%Y')

For more explanation about STR_TO_DATE and DATE function see the mysql documentation. Do not convert both values to string, because you can not compare it after the convertion