Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Sql Sqldate Mysql Date Previous Month

Related Tutorials


 
 

Share on Google+Share on Google+

Mysql Date Previous Month

Advertisement
Mysql Date Previous Month is used to return the current and previous value of the month.

Mysql Date Previous Month

     

Mysql Date Previous Month is used to return the current and previous value of the month.

Understand with Example

The Tutorial illustrate an example that help you to describe Previous Month value. To understand this, we have a month (now( )) that return you current month value.

Query for finding current month:-

 

 

 

mysql> select month(now()) as CurrentMonth;

Output:-

+--------------+
| CurrentMonth |
+--------------+
|           12 |
+--------------+
1 row in set (0.00 sec)

Query for finding Previous month:-

Once you are able to find the current month, you can easily find the previous month before the current month. The Query month(now( )) -1 return you the previous month value precede the current month.

month(now( ))-1 : The month(now( )) -1 return you the previous month  before the current month.

mysql> select month(now())-1 as PreviousMonth;

Output:-

+---------------+
| PreviousMonth |
+---------------+
|            11 |
+---------------+
1 row in set (0.00 sec)
Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: June 15, 2009

Related Tutorials

Discuss: Mysql Date Previous Month  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:5
manish
October 26, 2011
wrong way to do it

what if it is jan. then the answer will come 0
thanuditha
December 4, 2011
previous month

this has no point. it would work only for months greater than 1. if the current month is january it will display the previous month as 0 not 12. that doesnt make any sense
Beholder
December 9, 2011
And what about January?

January is month number 1. So 1 - 1 = 0!!! MySQL will freak out
Marc
January 21, 2013
It doesn't work with end of year

This solution fails in January, as that is month 1 and minus 1 it is month 0 and not 12, being December.
prageeth
June 12, 2012
get previous month in mysql

when the month is January it returns previous month as 0. how to get the previous month when selected month is january?
DMCA.com