Reputation: 3929
I was wondering which of the following is the preferred way to use datediff for datetime types.
datediff(current_date, <datetime type>)
datediff(current_date, date(<datetime type>))
datediff(now(), <datetime type>)
I tried them out on a few cases and they all returned the same answer but I'm worried that they may produce some error down the line.
Is datediff the correct way to find the number of days between two datetime types?
Upvotes: 0
Views: 234
Reputation: 425428
Given:
datediff
internally casts parameters to datenow()
cast to a date is the same as current_date
There should be no difference in the result between any of those combinations
Upvotes: 1
Reputation: 30088
For your purposes, they are all equivalent.
current_date returns a DATE type, while now() returns DATETIME
This doesn't matter in this case, though, because datediff() ignores the time part of a DATETIME, so using DATE and DATETIME with datediff() are completely equivalent.
Upvotes: 1