Show
4.4.5 Date CalculationsMySQL provides several functions that you can use to perform calculations on dates, for example, to calculate ages or extract parts of dates. To determine how many years old each of your pets is, use the
The query works, but the result could be scanned more easily if the rows were presented in some order. This can be done by adding an
To sort the
output by
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
The query uses 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
Finding animals with birthdays in the upcoming month is also simple. Suppose that the current month is April. Then the
month value is
There is a small complication if the current month is December. You cannot merely add one to the month number ( You can write the query so that it works no matter what the current month is, so that you do not have to use the number
for a particular month.
A different way to accomplish the same task is to add
If a calculation uses invalid dates, the calculation fails and produces warnings:
Which of the following functions is used to determine the number of months between two date values?You can use the DateDiff function to determine how many specified time intervals exist between two dates.
Which of the following clauses is used to display query results in sorted?12. Which of the following clauses is used to display query results in a sorted order? The correct answer should be ORDER BY not just order.
Which of the following symbols is a wildcard character that can represent any number of characters?Examples of wildcard characters. Which of the following symbols is used to represent a single character during a pattern search?An underscore ( _ ) in pattern stands for (matches) any single character; a percent sign ( % ) matches any sequence of zero or more characters.
|