List:General Discussion« Previous MessageNext Message »
From:Paul DuBois Date:March 2 2003 12:23am
Subject:Re: Days in a month
View as plain text  
At 17:13 -0700 3/1/03, Steffan A. Cline wrote:
>Is there a function that I missed for calculating the number of days in a
>month?

No, but you can calculate it.  One way:
- Find the first of the month
- Add a month
- Subtract a day.  That gives you the last day of the month.
- Extract the day-of-the-month

Example:

SET @d = '2003-03-01';
SELECT @d,
DATE_SUB(
         DATE_ADD(
                 DATE_SUB(@d,INTERVAL DAYOFMONTH(@d) - 1 DAY),
                 INTERVAL 1 MONTH),
         INTERVAL 1 DAY)
AS 'last of month';
SELECT @d,
DAYOFMONTH(
DATE_SUB(
         DATE_ADD(
                 DATE_SUB(@d,INTERVAL DAYOFMONTH(@d) - 1 DAY),
                 INTERVAL 1 MONTH),
         INTERVAL 1 DAY)
)
AS 'days in month';

Which produces this output:

+------------+---------------+
| @d         | last of month |
+------------+---------------+
| 2003-03-01 | 2003-03-31    |
+------------+---------------+
+------------+---------------+
| @d         | days in month |
+------------+---------------+
| 2003-03-01 |            31 |
+------------+---------------+

Easy! :-)

There are other solutions.

>
>
>Steffan
>
>
>
>MySQL
>---------------------------------------------------------------
>T E L  6 0 2 . 5 7 9 . 4 2 3 0 | F A X  6 0 2 . 9 7 1 . 1 6 9 4
>Steffan A. Cline
>Steffan@stripped                             Phoenix, Az
>http://www.ExecuChoice.net                                  USA
>AIM : SteffanC          ICQ : 57234309
>The Executive's Choice in Lasso driven Internet Applications
>---------------------------------------------------------------

Thread
Days in a monthSteffan A. Cline2 Mar
  • Re: Days in a monthPaul DuBois2 Mar
  • RE: Days in a monthDon Read2 Mar
    • problem: MySql: Got signal 11. Aborting!Dusan Pavlica3 Mar
      • Re: problem: MySql: Got signal 11. Aborting!miguel sol√≥rzano3 Mar