Section D.5. Functions


D.5. Functions

SQL is not a procedural language, but it does provide some data-transformation capabilities. In addition to the string concatenation operator (||), the SQL-92 specification defines two sets of functions: aggregate and value.

D.5.1. Aggregate Functions

In the section on the SELECT statement, we saw an aggregate function used to count the number of records within a group. Mainly aggregate functions act on all the records of query, counting rows, averaging fields, and so forth. For example, here's how to count the number of rows returned by a SELECT statement:

 SELECT COUNT(*) FROM CUSTOMERS 

Instead of returning each row of the CUSTOMERS table, this query returns a single-column, single-row result that contains the number of records in CUSTOMERS.

The other aggregate functions are AVG, SUM, MAX, and MIN. Unlike COUNT, which works on either a single column or all columns, the other functions work on only a single column. AVG and SUM can be applied against numerical data types only (integers, reals, etc.) while MAX and MIN work with any data type. Here are some examples:

 SELECT MIN(AGE) FROM GUESTS SELECT MAX(NAME) FROM GUESTS SELECT AVG(AGE), SUM(AGE) FROM GUESTS 

D.5.2. Value Functions

Value functions work on particular column values and return modified data. Some of them also generate values from system information.

D.5.2.1. Date/time functions

There are three date and time functions that retrieve the current date, current time, and current timestamp, respectively, from the database:

 CURRENT_DATE CURRENT_TIME[(precision)] CURRENT_TIMESTAMP[(precision)] 

CURRENT_TIME and CURRENT_TIMESTAMP accept an optional precision level, which specifies the decimal fractions of a second to be included in the time portion of the value. The current time zone is used with all these functions.

Here's how you might use these functions in a query:

 SELECT * FROM ORDERS WHERE ORDER_DATE = CURRENT_DATE INSERT INTO VISITORS (VISIT_TS) VALUES (CURRENT_TIMESTAMP) 

Some databases have platform-specific commands that duplicate this functionality (these commands often predate SQL -92). Oracle's SYSDATE is one example. Note that these functions aren't required for entry-level SQL-92.

D.5.2.2. String manipulation functions

The concatenation operator, ||, has been around since before the SQL-92 standard. It allows you to concatenate multiple column values and string literals. Say we have a table that contains FIRST_NAME and LAST_NAME fields, and we want to display them in a "last, first" form. Here's a SQL statement that returns a single column that does just that:

 SELECT LAST_NAME || ', ' || FIRST_NAME FROM CUSTOMERS 

In addition, the SQL-92 standard defines a number of other functions that can be used in SQL statements. UPPER and LOWER convert a column into uppercase or lowercase, respectively:

 SELECT UPPER(LAST_NAME) FROM CUSTOMERS SELECT LOWER(FIRST_NAME) FROM CUSTOMERS 

These functions can also be used in WHERE predicates, for example, to produce a case-insensitive search:

 SELECT * FROM CUSTOMERS WHERE UPPER(FIRST_NAME) LIKE 'WILL%' 

Most databases support UPPER and LOWER, but they are required only for full SQL-92 conformance, not entry-level conformance.

The TRIM function removes characters from one or both ends of a string:

 TRIM ([ [ LEADING | TRAILING | BOTH ] [ character ] FROM ] string ) 

Calling trIM on a string trims leading CURRENT_TIME and CURRENT_TIMESTAMP and trailing whitespace. Here's how to trim just leading blanks:

 SELECT TRIM(LEADING ' ' FROM FIRST_NAME) FROM CUSTOMERS 

And here's how to trim all "-" characters from both sides of a string:

 SELECT TRIM(BOTH '-' FROM FIRST_NAME) FROM CUSTOMERS 

Like UPPER and LOWER, trIM is required only for full SQL-92 conformance, although it is supported by most database implementations.

The SUBSTRING command extracts a given number of characters from a larger string. It is defined as:

 SUBSTRING (source_string FROM start_pos FOR number_of_characters) 

For example, to get each customer's initials, we might use the following query:

 SELECT SUBSTRING (FIRST_NAME FROM 1 FOR 1),   SUBSTRING(LAST_NAME FROM 1 FOR 1) 

SUBSTRING is required only for intermediate-level SQL-92 conformance.



Java Enterprise in a Nutshell
Java Enterprise in a Nutshell (In a Nutshell (OReilly))
ISBN: 0596101422
EAN: 2147483647
Year: 2004
Pages: 269

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net