|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
One item in Table 11.1 requires further explanation. SOUNDEX is an algorithm that converts any string of text into an alphanumeric pattern describing the phonetic representation of that text. SOUNDEX takes into account similar sounding characters and syllables, enabling strings to be compared by how they sound rather than how they have been typed. Although SOUNDEX is not a SQL concept, MySQL (like many other DBMSs) offers SOUNDEX support. Here's an example using the Soundex() function. Customer Coyote Inc. is in the customers table and has a contact named Y. Lee. But what if that were a typo, and the contact actually was supposed to have been Y. Lie? Obviously, searching by the correct contact name would return no data, as shown here: • Input SELECT cust_name, cust_contact
• Output +-------------+--------------+
• Input SELECT cust_name, cust_contact
• Output +-------------+--------------+
• Analysis In this example, the WHERE clause uses the Soundex() function to convert both the cust_contact column value and the search string to their SOUNDEX values. Because Y. Lee and Y. Lie sound alike, their SOUNDEX values match, and so the WHERE clause correctly filtered the desired data.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Function |
Description |
|---|---|
|
AddDate() |
Add to a date (days, weeks, and so on) |
|
AddTime() |
Add to a time (hours, minutes, and so on) |
|
CurDate() |
Returns the current date |
|
CurTime() |
Returns the current time |
|
Date() |
Returns the date portion of a date time |
|
DateDiff() |
Calculates the difference between two dates |
|
Date_Add() |
Highly flexible date arithmetic function |
|
Date_Format() |
Returns a formatted date or time string |
|
Day() |
Returns the day portion of a date |
|
DayOfWeek() |
Returns the day of week for a date |
|
Hour() |
Returns the hour portion of a time |
|
Minute() |
Returns the minute portion of a time |
|
Month() |
Returns the month portion of a date |
|
Now() |
Returns the current date and time |
|
Second() |
Returns the second portion of a time |
|
Time() |
Returns the time portion of a date time |
|
Year() |
Returns the year portion of a date |
This would be a good time to revisit data filtering using WHERE. Thus far we have filtered data using WHERE clauses that compared numbers and text, but frequently data needs to be filtered by date. Filtering by date requires some extra care, and the use of special MySQL functions.
The first thing to keep in mind is the date format used by MySQL. Whenever you specify a date, be it inserting or updating table values, or filtering using WHERE clauses, the date must be in the format yyyy-mm-dd. So, for September 1st, 2005 specify 2005-09-01. Although other date formats might be recognized, this is the preferred date format because it eliminates ambiguity (after all, is 04/05/06 May 4th 2006, or April 5th 2006, or May 6th 2004, or... you get the idea).
Tip
Always Use Four-Digit Years Two-digit years are supported, and MySQL treats years 00-69 as 2000-2069 and 70-99 as 1970-1999. While these might in fact be the intended years, it is far safer to always use a full four-digit year so MySQL does not have to make any assumptions for you.
As such, a basic date comparison should be simple enough:
• Input
SELECT cust_id, order_num
FROM orders
WHERE order_date = '2005-09-01';

• Output
+---------+-----------+
| cust_id | order_num |
+---------+-----------+
| 10001 | 20005 |
+---------+-----------+
• Analysis
That SELECT statement worked; it retrieved a single order record, one with an order_date of 2005-09-01.
But is using WHERE order_date = '2005-09-01' safe? order_date has a datatype of datetime. This type stores dates along with time values. The values in our example tables all have times of 00:00:00, but that might not always be the case. What if order dates were stored using the current date and time (so you'd not only know the order date but also the time of day that the order was placed)? Then WHERE order_date = '2005-09-01' fails if, for example, the stored order_date value is 2005-09-01 11:30:05. Even though a row with that date is present, it is not retrieved because the WHERE match failed.
The solution is to instruct MySQL to only compare the specified date to the date portion of the column instead of using the entire column value. To do this you must use the Date() function. Date(order_date) instructs MySQL to extract just the date part of the column, and so a safer SELECT statement is
• Input
SELECT cust_id, order_num
FROM orders
WHERE Date(order_date) = '2005-09-01';

Tip
If You Mean Date Use Date() It's a good practice to use Date() if what you want is just the date, even if you know that the column only contains dates. This way, if somehow a date time value ends up in the table in the future, your SQL won't break. Oh, and yes, there is a Time() function, too, and it should be used when you want the time.
Both Date() and Time() were first introduced in MySQL 4.1.1.
Now that you know how to use dates to test for equality, using all of the other operators (introduced in Tutorial 11, "Filtering Data") should be self-explanatory.
But one other type of date comparison warrants explanation. What if you wanted to retrieve all orders placed in September 2005? A simple equality test does not work as it matches the day of month, too. There are several solutions, one of which follows:
• Input
SELECT cust_id, order_num
FROM orders
WHERE Date(order_date) BETWEEN '2005-09-01' AND
'2005-09-30';

• Output
+---------+-----------+
| cust_id | order_num |
+---------+-----------+
| 10001 | 20005 |
| 10003 | 20006 |
| 10004 | 20007 |
+---------+-----------+
Function |
Description |
|---|---|
Abs() |
Returns a number's absolute value |
Cos() |
Returns the trigonometric cosine of a specified angle |
Exp() |
Returns the exponential value of a specific number |
Mod() |
Returns the remainder of a division operation |
Pi() |
Returns the value of pi |
Rand() |
Returns a random number |
Sin() |
Returns the trigonometric sine of a specified angle |
Sqrt() |
Returns the square root of a specified number |
Tan() |
Returns the trigonometric tangent of a specified angle |
In this tutorial, you learned how to use SQL's data manipulation functions, and paid special attention to working with dates.
[Next]