Number of days between current date and date field

回眸只為那壹抹淺笑 提交于 2019-12-08 18:58:54

问题


I have this problem if anyone can help. There is a field (date) in my table (table1) that is a date in the format 3/31/1988 (M/D/y), and my necessity is to define how many days have passed since that date.

I have tried to give this instruction

SELECT DATEDIFF(CURDATE(), date) AS days
FROM table1

But it gives back 'null' and I think this happens because the two date formats are different (CURDATE() is YMD.....

Is it correct? can anyone help me? Thank you in advance


回答1:


You can use STR_TO_DATE():

SELECT DATEDIFF(CURDATE(),STR_TO_DATE(date, '%m/%d/%Y')) AS days
FROM table1

SQLFiddle Demo




回答2:


Your DATE field should have DATE or DATETIME format to be used as DATEDIFF argument correctly.

Also DATE is MySQL keyword and I am not sure that you can use it as valid field name.




回答3:


You can use this for accurate result

SELECT DATEDIFF(CURDATE(), DATE_FORMAT(FROM_UNIXTIME(UNIX_TIMESTAMP(`date`)), '%Y-%m-%d')) AS days FROM `table1`


来源:https://stackoverflow.com/questions/17179850/number-of-days-between-current-date-and-date-field

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