Beginning SQL

Beginning SQL Beginning SQL

marjan.fesb.hr
from marjan.fesb.hr More from this publisher
20.07.2013 Views

Chapter 5 Date Functions 178 Sometimes it’s quite useful to be able to extract individual parts of a date, such as day of the month, month, or year. To do this, SQL provides the DAY(), MONTH(), and YEAR() functions. These are supported by all of the database systems except Oracle. These functions perform in the same manner, and the syntax for each is as follows: DAY(date) MONTH(date) YEAR(date) The following code displays the DateOfBirth of each film club member, as well as the day of the month, month, and year of their birth: SELECT DateOfBirth, DAY(DateOfBirth), MONTH(DateOfBirth), YEAR(DateOfBirth) FROM MemberDetails ORDER BY YEAR(DateOfBirth); Executing the preceding statement provides the following results: DateOfBirth DAY(DateOfBirth) MONTH(DateOfBirth) YEAR(DateOfBirth) 1937-01-20 20 1 1937 1945-06-09 9 6 1945 1952-10-05 5 10 1952 1953-08-25 25 8 1953 1956-08-07 7 8 1956 1956-10-21 21 10 1956 1967-10-05 5 10 1967 1974-05-27 27 5 1974 1977-01-09 9 1 1977 1992-07-17 17 7 1992 1994-05-28 28 5 1994 1997-05-28 28 5 1997 Notice the use of a date function in the ORDER BY clause, which allows you to order the results (by year in this case). The first row of the results is empty because the DateOfBirth column in one of the rows contains a NULL value. Most database systems contain a large number of date- and time-related functions, so it’s worth checking the database help files or product documentation to see what particular functions your system has.

Chapter 5<br />

Date Functions<br />

178<br />

Sometimes it’s quite useful to be able to extract individual parts of a date, such as day of the month,<br />

month, or year. To do this, <strong>SQL</strong> provides the DAY(), MONTH(), and YEAR() functions. These are supported<br />

by all of the database systems except Oracle.<br />

These functions perform in the same manner, and the syntax for each is as follows:<br />

DAY(date)<br />

MONTH(date)<br />

YEAR(date)<br />

The following code displays the DateOfBirth of each film club member, as well as the day of the month,<br />

month, and year of their birth:<br />

SELECT DateOfBirth, DAY(DateOfBirth), MONTH(DateOfBirth), YEAR(DateOfBirth)<br />

FROM MemberDetails<br />

ORDER BY YEAR(DateOfBirth);<br />

Executing the preceding statement provides the following results:<br />

DateOfBirth DAY(DateOfBirth) MONTH(DateOfBirth) YEAR(DateOfBirth)<br />

1937-01-20 20 1 1937<br />

1945-06-09 9 6 1945<br />

1952-10-05 5 10 1952<br />

1953-08-25 25 8 1953<br />

1956-08-07 7 8 1956<br />

1956-10-21 21 10 1956<br />

1967-10-05 5 10 1967<br />

1974-05-27 27 5 1974<br />

1977-01-09 9 1 1977<br />

1992-07-17 17 7 1992<br />

1994-05-28 28 5 1994<br />

1997-05-28 28 5 1997<br />

Notice the use of a date function in the ORDER BY clause, which allows you to order the results (by year<br />

in this case). The first row of the results is empty because the DateOfBirth column in one of the rows<br />

contains a NULL value.<br />

Most database systems contain a large number of date- and time-related functions, so it’s worth checking<br />

the database help files or product documentation to see what particular functions your system has.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!