Table 8.2. Commonly Used Text-Manipulation Functions | |
Function | Description |
LEFT() (or use substring function) | |
LENGTH() (also DATALENGTH() or LEN()) | |
LOWER() | |
LTRIM() (LCASE() if using Access) | |
RIGHT() (or use substring function) | |
RTRIM() | |
SOUNDEX() | |
UPPER() (UCASE() if using Access) |
SELECT cust_name, cust_contact
FROM Customers
WHERE SOUNDEX(cust_contact) = SOUNDEX('Michael Green');
日期转换
SELECT order_num
FROM Orders
WHERE DATEPART(yy, order_date) = 2004;
In Access use this version:
SELECT order_num
FROM Orders
WHERE DATEPART('yyyy', order_date) = 2004;
Here is the PostgreSQL version that uses a similar function named DATE_PART():
SELECT order_num
FROM Orders
WHERE DATE_PART('year', order_date) = 2004;
MySQL has all sorts of date manipulation functions, but not DATEPART(). MySQL users can use a function named YEAR() to extract the year from a date:
SELECT order_num
FROM Orders
WHERE YEAR(order_date) = 2004;
Oracle has no DATEPART() function either, but there are several other date manipulation functions that can be used to accomplish the same retrieval. Here is an example:
SELECT order_num
FROM Orders
WHERE to_number(to_char(order_date, 'YY')) = 2004;
SELECT order_num
FROM Orders
WHERE order_date BETWEEN to_date('01-JAN-2004')
AND to_date('31-DEC-2004');
In this example, Oracle's to_date() function is used to convert two strings to dates. One contains the date January 1, 2004, and the other contains the date December 31, 2004. A standard BETWEEN operator is used to find all orders between those two dates. It is worth noting that this same code would not work with SQL Server because it does not support the to_date() function. However, if you replaced to_date() with DATEPART(), you could indeed use this type of statement.
Table 8.3. Commonly Used Numeric Manipulation Functions | |
Function | Description |
ABS() | |
COS() | |
EXP() | |
PI() | |
SIN() | |
SQRT() | |
TAN() |
Refer to your DBMS documentation for a list of the supported mathematical manipulation functions