Microsoft Word - Sam's Teach Yourself MySQL in 21 Days - SAMS.doc

(singke) #1
| NULL |

+----------------+

Extracting Years, Quarters, Months, and Weeks


MySQL has a range of function for extracting numerical data from a date:
ƒ YEAR(date)
ƒ QUARTER(date)
ƒ MONTH(date)
ƒ WEEK(date[,firstday])
ƒ YEARWEEK(date[,firstday])
YEAR(date) returns the 4-digit numeric year of a given date:
mysql> SELECT YEAR('01/12/25 11:00:00');


results in the following:
+-----------------------------+
| YEAR('2001/12/25 11:00:00') |
+-----------------------------+
| 2001 |
+-----------------------------+
QUARTER(date) the range 1 to 4:
mysql> SELECT QUARTER('01/12/25 11:00:00');

results in the following:
+------------------------------+
| QUARTER('01/12/25 11:00:00') |
+------------------------------+
| 4 |
+------------------------------+
MONTH(date) returns the month in the range 1 to 12:
mysql> SELECT MONTH('01/12/25 11:00:00');

results in the following:
+----------------------------+
| MONTH('01/12/25 11:00:00') |
+----------------------------+
| 12 |
+----------------------------+
WEEK(date), parameter, returns the week of the year in the range 0 to 53:
mysql> SELECT WEEK('2001-12-26');

results in the following:
+--------------------+
| WEEK('2001-12-26') |
+--------------------+
| 52 |
+--------------------+
Without a second parameter, WEEK(date) assumes that Sunday is the first day of the week, and, at
the beginning of the year, any days before the 'first day' come in week 0. For example, WEEK('2000-
01-01') returns 0 because it was a Saturday.
Free download pdf