MySQL Forums
Forum List  »  Newbie

Re: Query for 3 months back
Posted by: Barry Galbraith
Date: January 07, 2013 02:40PM

Um, If I subtract 3 months from January, I get the correct date in October, allowing 31 days for December and October.

mysql> select date_sub(now(),interval 3 month), now();
+----------------------------------+---------------------+
| date_sub(now(),interval 3 month) | now()               |
+----------------------------------+---------------------+
| 2012-10-08 07:38:19              | 2013-01-08 07:38:19 |
+----------------------------------+---------------------+
1 row in set (0.00 sec)

Even does good stuff like this
mysql> select date_sub('2013-03-29',Interval 1 month);
+-----------------------------------------+
| date_sub('2013-03-29',Interval 1 month) |
+-----------------------------------------+
| 2013-02-28                              |
+-----------------------------------------+
1 row in set (0.05 sec)

mysql> select date_sub('2012-12-31',Interval 1 month);
+-----------------------------------------+
| date_sub('2012-12-31',Interval 1 month) |
+-----------------------------------------+
| 2012-11-30                              |
+-----------------------------------------+
1 row in set (0.00 sec)

Maybe something else is a problem.

Good luck,
Barry.



Edited 1 time(s). Last edit at 01/07/2013 06:11PM by Barry Galbraith.

Options: ReplyQuote


Subject
Written By
Posted
January 07, 2013 12:49PM
Re: Query for 3 months back
January 07, 2013 02:40PM
January 08, 2013 09:23AM
January 08, 2013 09:26AM
January 08, 2013 03:04PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.