2

In mysql, we can do the following for adding months:

SELECT DATE_ADD('2014-08-20', INTERVAL 13 MONTH); //Result: 2015-09-20

Is there any way to do the reversed operation ? Example:

SELECT DIFF_IN_MONTHS('2015-09-20', '2014-08-20') //Result: 13

Roundings due to day differences are not a problem for me.

Bob
  • 2,430
  • 22
  • 27
  • 2
    possible duplicate of [Difference between two dates in MySQL](http://stackoverflow.com/questions/4759248/difference-between-two-dates-in-mysql) – idmean Aug 20 '14 at 16:12

2 Answers2

3

The function TIMESTAMPDIFF does this:

SELECT TIMESTAMPDIFF(MONTH, '2015-09-20', '2014-08-20');

TIMESTAMPDIFF(unit,datetime_expr1,datetime_expr2)

Returns datetime_expr2 – datetime_expr1, where datetime_expr1 and datetime_expr2 are date or datetime expressions. One expression may be a date and the other a datetime; a date value is treated as a datetime having the time part '00:00:00' where necessary. The unit for the result (an integer) is given by the unit argument

VMai
  • 10,156
  • 9
  • 25
  • 34
0

So you are wanting to get the difference in days between the 2 dates?

    DATEDIFF('1015-09-20','2014-08-20')
user3385236
  • 49
  • 1
  • 6