MySQL convert between two date format

只愿长相守 提交于 2019-12-19 11:54:34

问题


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



来源:https://stackoverflow.com/questions/43658624/mysql-convert-between-two-date-format

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!