MySQL Cookbook
Authors: DuBois P.
Published year: 2005
Pages: 129-130/412
Buy this book on amazon.com >>

5.23 Finding the Length of a Month

5.23.1 Problem

You want to know how many days there are in a month.

5.23.2 Solution

Determine the date of its last day, then extract the day-of-month component from the result.

5.23.3 Discussion

To determine the number of days for the month in which a given date occurs, calculate the date for the last day of the month as shown in the previous section, then extract the DAYOFMONTH( ) value from the result:

mysql>

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'

->

FROM date_val;

+------------+---------------+
 d           days in month 
+------------+---------------+
 1864-02-28             29 
 1900-01-15             31 
 1987-03-05             31 
 1999-12-31             31 
 2000-06-04             30 
+------------+---------------+

5.23.4 See Also

Recipe 5.28 later in this chapter discusses another way to calculate month lengths. Chapter 10 discusses leap year calculations in the context of date validation.


5.24 Calculating One Date from Another by Substring Replacement

5.24.1 Problem

Given a date, you want to produce another date from it, and you know the two dates share some components in common.

5.24.2 Solution

Treat a date or time value as a string and perform direct replacement on parts of the string.

5.24.3 Discussion

In some cases, you can use substring replacement to calculate dates without performing any date arithmetic. For example, you can use string operations to produce the first-of-month value for a given date by replacing the day component with 01 . You can do this either with DATE_FORMAT( ) or with CONCAT( ) :

mysql>

SELECT d,

->

DATE_FORMAT(d,'%Y-%m-01') AS method1,

->

CONCAT(YEAR(d),'-',LPAD(MONTH(d),2,'0'),'-01') AS method2

->

FROM date_val;

+------------+------------+------------+
 d           method1     method2    
+------------+------------+------------+
 1864-02-28  1864-02-01  1864-02-01 
 1900-01-15  1900-01-01  1900-01-01 
 1987-03-05  1987-03-01  1987-03-01 
 1999-12-31  1999-12-01  1999-12-01 
 2000-06-04  2000-06-01  2000-06-01 
+------------+------------+------------+

The string replacement technique can also be used to produce dates with a specific position within the calendar year. For New Year's Day (January 1), replace the month and day with 01 :

mysql>

SELECT d,

->

DATE_FORMAT(d,'%Y-01-01') AS method1,

->

CONCAT(YEAR(d),'-01-01') AS method2

->

FROM date_val;

+------------+------------+------------+
 d           method1     method2    
+------------+------------+------------+
 1864-02-28  1864-01-01  1864-01-01 
 1900-01-15  1900-01-01  1900-01-01 
 1987-03-05  1987-01-01  1987-01-01 
 1999-12-31  1999-01-01  1999-01-01 
 2000-06-04  2000-01-01  2000-01-01 
+------------+------------+------------+

For Christmas, replace the month and day with 12 and 25 :

mysql>

SELECT d,

->

DATE_FORMAT(d,'%Y-12-25') AS method1,

->

CONCAT(YEAR(d),'-12-25') AS method2

->

FROM date_val;

+------------+------------+------------+
 d           method1     method2    
+------------+------------+------------+
 1864-02-28  1864-12-25  1864-12-25 
 1900-01-15  1900-12-25  1900-12-25 
 1987-03-05  1987-12-25  1987-12-25 
 1999-12-31  1999-12-25  1999-12-25 
 2000-06-04  2000-12-25  2000-12-25 
+------------+------------+------------+

To perform the same operation for Christmas in other years, combine string replacement with date shifting. The following query shows two ways to determine the date for Christmas two years hence. The first method finds Christmas for this year, then shifts it two years forward. The second shifts the current date forward two years , then finds Christmas in the resulting year:

mysql>

SELECT CURDATE( ),

->

DATE_ADD(DATE_FORMAT(CURDATE( ),'%Y-12-25'),INTERVAL 2 YEAR)

->

AS method1,

->

DATE_FORMAT(DATE_ADD(CURDATE( ),INTERVAL 2 YEAR),'%Y-12-25')

->

AS method2;

+------------+------------+------------+
 CURDATE( )   method1     method2    
+------------+------------+------------+
 2002-07-15  2004-12-25  2004-12-25 
+------------+------------+------------+
MySQL Cookbook
Authors: DuBois P.
Published year: 2005
Pages: 129-130/412
Buy this book on amazon.com >>

Similar books on Amazon