user1832628
user1832628

Reputation: 3929

Mysql datediff for datetime types

I was wondering which of the following is the preferred way to use datediff for datetime types.

  1. datediff(current_date, <datetime type>)
  2. datediff(current_date, date(<datetime type>))
  3. 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

Answers (2)

Bohemian
Bohemian

Reputation: 425428

Given:

  • datediff internally casts parameters to date
  • now() 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

GreyBeardedGeek
GreyBeardedGeek

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

Related Questions