Parse Date from MM/DD/YYYY to a format MYSQL can work with

早过忘川 提交于 2019-12-31 07:01:16

问题


Currently working with a MLS data dump in which all their dates are formatted in MM/DD/YYYY , Trying to get the min/max age

        MIN( DateDiff( NOW(), idx_common.list_date ))) AS listage_min,
        MAX( DateDiff( NOW(), idx_common.list_date ))) AS listage_max

Obviously this does not work because idx_common.list_date is in said format.

Any ideas?


回答1:


Use str_to_date to convert idx_common.list_date.

MIN( DateDiff( NOW(), str_to_date(idx_common.list_date,'%m/%d/%Y') ))) AS listage_min,
MAX( DateDiff( NOW(), str_to_date(idx_common.list_date,'%m/%d/%Y') ))) AS listage_max



回答2:


http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_str-to-date contains the answer you seek.

However - please consider using real DATETIME columns instead of strings in your database, if at all possible.




回答3:


use java or c# or something to split the string of the date by the / and reorder the date into the format expected by mysql before you run the mysql query.



来源:https://stackoverflow.com/questions/4630208/parse-date-from-mm-dd-yyyy-to-a-format-mysql-can-work-with

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