Oracle 5.0 Reference Manual page 226

Table of Contents

Advertisement

| name
| birth
+----------+------------+------------+------+
| Bowser
| 1989-08-31 | 2003-08-19 |
| Buffy
| 1989-05-13 | 2003-08-19 |
| Chirpy
| 1998-09-11 | 2003-08-19 |
| Claws
| 1994-03-17 | 2003-08-19 |
| Fang
| 1990-08-27 | 2003-08-19 |
| Fluffy
| 1993-02-04 | 2003-08-19 |
| Puffball | 1999-03-30 | 2003-08-19 |
| Slim
| 1996-04-29 | 2003-08-19 |
| Whistler | 1997-12-09 | 2003-08-19 |
+----------+------------+------------+------+
To sort the output by
age
mysql>
SELECT name, birth, CURDATE(),
->
TIMESTAMPDIFF(YEAR,birth,CURDATE()) AS age
->
FROM pet ORDER BY age;
+----------+------------+------------+------+
| name
| birth
+----------+------------+------------+------+
| Chirpy
| 1998-09-11 | 2003-08-19 |
| Puffball | 1999-03-30 | 2003-08-19 |
| Whistler | 1997-12-09 | 2003-08-19 |
| Slim
| 1996-04-29 | 2003-08-19 |
| Claws
| 1994-03-17 | 2003-08-19 |
| Fluffy
| 1993-02-04 | 2003-08-19 |
| Fang
| 1990-08-27 | 2003-08-19 |
| Bowser
| 1989-08-31 | 2003-08-19 |
| Buffy
| 1989-05-13 | 2003-08-19 |
+----------+------------+------------+------+
A similar query can be used to determine age at death for animals that have died. You determine
which animals these are by checking whether the
values, compute the difference between the
mysql>
SELECT name, birth, death,
->
TIMESTAMPDIFF(YEAR,birth,death) AS age
->
FROM pet WHERE death IS NOT NULL ORDER BY age;
+--------+------------+------------+------+
| name
| birth
+--------+------------+------------+------+
| Bowser | 1989-08-31 | 1995-07-29 |
+--------+------------+------------+------+
The query uses
death IS NOT NULL
value that cannot be compared using the usual comparison operators. This is discussed later. See
Section 3.3.4.6, "Working with
What if you want to know which animals have birthdays next month? For this type of calculation,
year and day are irrelevant; you simply want to extract the month part of the
provides several functions for extracting parts of dates, such as
[923].
DAYOFMONTH()
simple query that displays the value of both
mysql>
SELECT name, birth, MONTH(birth) FROM pet;
+----------+------------+--------------+
| name
| birth
+----------+------------+--------------+
| Fluffy
| 1993-02-04 |
| Claws
| 1994-03-17 |
| Buffy
| 1989-05-13 |
| Fang
| 1990-08-27 |
| Bowser
| 1989-08-31 |
| Chirpy
| 1998-09-11 |
| Whistler | 1997-12-09 |
| Slim
| 1996-04-29 |
| Puffball | 1999-03-30 |
+----------+------------+--------------+
Retrieving Information from a Table
| CURDATE()
| age
13 |
14 |
4 |
9 |
12 |
10 |
4 |
7 |
5 |
rather than name, just use a different
| CURDATE()
| age
4 |
4 |
5 |
7 |
9 |
10 |
12 |
13 |
14 |
death
| death
| age
|
5 |
rather than
Values".
NULL
[925]
is the appropriate function here. To see how it works, run a
MONTH()
birth
| MONTH(birth) |
2 |
3 |
5 |
8 |
8 |
9 |
12 |
4 |
3 |
206
|
ORDER BY
|
value is NULL. Then, for those with
death
and
values:
birth
because
death <> NULL
YEAR()
and
MONTH(birth)
clause:
non-NULL
is a special
NULL
column. MySQL
birth
[933],
[925], and
MONTH()
[925]:

Advertisement

Table of Contents
loading

This manual is also suitable for:

Mysql 5.0

Table of Contents