Beginning SQL
Beginning SQL Beginning SQL
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.
- Page 344: Chapter 4 152 SELECT * FROM MemberD
- Page 348: Chapter 4 ❑ Choose meaningful nam
- Page 354: 5 Manipulating Data In Chapter 3, y
- Page 358: The results of this query are as fo
- Page 362: You can raise a number to any power
- Page 366: To demonstrate the sort of results
- Page 370: The CEILING() Function The CEILING(
- Page 374: ased on the digits after the decima
- Page 378: The SUBSTRING() function works at t
- Page 382: LastName UPPER(LastName) LOWER(Last
- Page 386: The LENGTH() function takes just on
- Page 390: LastName LOWER(SUBSTRING(LastName,1
- Page 394: FirstName DIFFERENCE(FirstName, ‘
- Page 400: Chapter 5 The syntax for the CAST()
- Page 404: Chapter 5 NULLs and Strings 182 NUL
- Page 408: Chapter 5 184 SELECT FirstName, Las
- Page 412: Chapter 5 186 This SQL copies over
- Page 416: Chapter 5 You also learned that mat
- Page 420: Chapter 6 190 Begin by looking at h
- Page 424: Chapter 6 This section covers queri
- Page 428: Chapter 6 194 This query gives the
- Page 432: Chapter 6 You’re almost there. Yo
- Page 436: Chapter 6 the database even though
- Page 440: Chapter 6 SELECT Category, AVG(DVDP
- Page 444: Chapter 6 Try It Out Selecting Data
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.