Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Number of days between current date and date field

Tags:

mysql

datediff

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

like image 399
user1951561 Avatar asked Jun 18 '13 22:06

user1951561


People also ask

How do you calculate days between two dates?

Calculate the no. of days between two dates, divide the time difference of both the dates by no. of milliseconds in a day (1000*60*60*24) Print the final result using document.

How do you make Excel automatically count days from a specific date?

To find the number of days between these two dates, you can enter “=B2-B1” (without the quotes into cell B3). Once you hit enter, Excel will automatically calculate the number of days between the two dates entered.

How do I calculate days difference in Excel?

Just subtract one date from the other. For example if cell A2 has an invoice date in it of 1/1/2015 and cell B2 has a date paid of 1/30/2015, then you could enter use the formula =B2-A2 to get the number of days between the two dates, or 29.


2 Answers

You can use STR_TO_DATE():

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

SQLFiddle Demo

like image 158
DarkAjax Avatar answered Sep 24 '22 03:09

DarkAjax


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.

like image 35
zavg Avatar answered Sep 24 '22 03:09

zavg