| Oracle8i SQL Reference Release 2 (8.1.6) A76989-01 |
|
Functions, 2 of 121
SQL functions are built into Oracle and are available for use in various appropriate SQL statements. Do not confuse SQL functions with user functions written in PL/SQL. User functions are described in "User-Defined Functions". For information about functions used with Oracle interMedia, see Oracle8i interMedia Audio, Image, and Video User's Guide and Reference.
If you call a SQL function with an argument of a datatype other than the datatype expected by the SQL function, Oracle implicitly converts the argument to the expected datatype before performing the SQL function. See "Data Conversion".
If you call a SQL function with a null argument, the SQL function automatically returns null. The only SQL functions that do not necessarily follow this behavior are CONCAT, NVL, and REPLACE.
In the syntax diagrams for SQL functions, arguments are indicated by their datatypes, following the conventions described in "Syntax Diagrams and Notation" in the Preface of this reference. When the parameter "function" appears in SQL syntax, replace it with one of the functions described in this section. Functions are grouped by the datatypes of their arguments and their return values. The general syntax is as follows:
function::=
single_row_function::=
The sections that follow list the built-in SQL functions in each of the groups illustrated above except user-defined functions. All of the built-in SQL functions are then described in alphabetical order. User-defined functions are described at the end of this chapter.
The examples provided with the function descriptions use the EMP and DEPT tables that are part of the SCOTT schema in your sample Oracle database. Many examples also use a SALES table, which has the following contents:
REGION PRODUCT S_DAY S_MONTH S_YEAR S_AMOUNT S_PROFIT ------ ------- ------ ---------- ---------- ---------- -------- 200 1 10 6 1998 77586 586 200 1 26 8 1998 62109 509 200 1 11 11 1998 46632 432 200 1 14 4 1999 15678 278 201 1 9 6 1998 77972 587 201 1 25 8 1998 62418 510 201 1 10 11 1998 46864 433 201 1 13 4 1999 15756 279 200 2 9 6 1998 39087 293.5 200 2 25 8 1998 31310 255 200 2 10 11 1998 23533 216.5 200 2 13 4 1999 7979 139.5 201 2 9 11 1998 23649.5 217 201 2 12 4 1999 8018.5 140 200 3 9 11 1998 15834 144.67 200 3 12 4 1999 5413.33 93.33 201 3 11 4 1999 5440 93.67 200 4 11 4 1999 4131 70.25 201 4 10 4 1999 4151.25 70.5 200 5 10 4 1999 3362 56.4 201 5 5 6 1998 16068 118.2 201 5 21 8 1998 12895.6 102.8 201 5 9 4 1999 3378.4 56.6
Single-row functions return a single result row for every row of a queried table or view. These functions can appear in select lists, WHERE clauses, START WITH clauses, and CONNECT BY clauses.
Number functions accept numeric input and return numeric values. Most of these functions return values that are accurate to 38 decimal digits. The transcendental functions COS, COSH, EXP, LN, LOG, SIN, SINH, SQRT, TAN, and TANH are accurate to 36 decimal digits. The transcendental functions ACOS, ASIN, ATAN, and ATAN2 are accurate to 30 decimal digits. The number functions are:
Character functions that return character values, unless otherwise noted, return values with the datatype VARCHAR2 and are limited in length to 4000 bytes. Functions that return values of datatype CHAR are limited in length to 2000 bytes. If the length of the return value exceeds the limit, Oracle truncates it and returns the result without an error message. The character functions that return character values are:
The character functions that return number values are:
Date functions operate on values of the DATE datatype. All date functions return a value of DATE datatype, except the MONTHS_BETWEEN function, which returns a number. The date functions are:
Conversion functions convert a value from one datatype to another. Generally, the form of the function names follows the convention datatype TO datatype. The first datatype is the input datatype. The second datatype is the output datatype. The SQL conversion functions are:
The following single-row functions do not fall into any of the other single-row function categories.
Aggregate functions return a single result row based on groups of rows, rather than on single rows. Aggregate functions can appear in select lists and in ORDER BY and HAVING clauses. They are commonly used with the GROUP BY clause in a SELECT statement, where Oracle divides the rows of a queried table or view into groups. In a query containing a GROUP BY clause, the elements of the select list can be aggregate functions, GROUP BY expressions, constants, or expressions involving one of these. Oracle applies the aggregate functions to each group of rows and returns a single result row for each group.
If you omit the GROUP BY clause, Oracle applies aggregate functions in the select list to all the rows in the queried table or view. You use aggregate functions in the HAVING clause to eliminate groups from the output based on the results of the aggregate functions, rather than on the values of the individual rows of the queried table or view.
|
See Also:
In the description of |
Many (but not all) aggregate functions that take a single argument accept these options:
DISTINCT causes an aggregate function to consider only distinct values of the argument expression.
ALL causes an aggregate function to consider all values, including all duplicates.
For example, the DISTINCT average of 1, 1, 1, and 3 is 2. The ALL average is 1.5. If you specify neither option, the default is ALL.
All aggregate functions except COUNT(*) and GROUPING ignore nulls. You can use the NVL function in the argument to an aggregate function to substitute a value for a null. COUNT never returns null, but returns either a number or zero. For all the remaining aggregate functions, if the data set contains no rows, or contains only rows with nulls as arguments to the aggregate function, then the function returns null.
The aggregate functions are:
Analytic functions compute an aggregate value based on a group of rows. The group of rows is called a window and is defined by the analytic clause. For each row, a "sliding" window of rows is defined. The window determines the range of rows used to perform the calculations for the "current row". Window sizes can be based on either a physical number of rows or a logical interval such as time.
Analytic functions are the last set of operations performed in a query except for the final ORDER BY clause. All joins and all WHERE, GROUP BY, and HAVING clauses are completed before the analytic functions are processed. Therefore, analytic functions can appear only in the select list or ORDER BY clause.
Analytic functions are commonly used to compute cumulative, moving, centered, and reporting aggregates.
analytic_function::=
analytic_clause::=
The keywords and parameters of this syntax are:
|
analytic_function |
is the name of an analytic function (see the listings of different types of analytic functions following this table). |
|
arguments |
Analytic functions take 0 to 3 arguments. |
|
analytic_clause |
indicates that the function operates on a query result set. That is, it is computed after the |
|
query_partition_clause |
|
|
|
partitions the query result set into groups based on one or more value_expr. If you omit this clause, the function treats all rows of the query result set as a single group.
You can specify multiple analytic functions in the same query, each with the same or different |
|
|
Note: If the objects being queried have the parallel attribute, and if you specify an analytic function with the query_partition_clause, then the function computations are parallelized as well. |
|
value_expr |
Valid value expressions are constants, columns, nonanalytic functions, function expressions, or expressions involving any of these. |
|
ORDER_BY_clause |
|
|
ORDER BY |
specifies how data is ordered within a partition. You can order the values in a partition on multiple keys, each defined by a value_expr and each qualified by an ordering sequence. Within each function, you can specify multiple ordering expressions. Doing so is especially useful when using functions that rank values, because the second expression can resolve ties between identical values for the first expression. See Also: order_by_clause of "SELECT and Subqueries" for more information on this clause. |
|
|
Note: Analytic functions always operate on rows in the order specified in the ORDER_BY_clause of the function. However, the ORDER_BY_clause of the function does not guarantee the order of the result. Use the ORDER_BY_clause of the query to guarantee the final result ordering. |
|
|
Restriction: When used in an analytic function, the ORDER_BY_clause must take an expression (expr). Position (position) and column aliases (c_alias) are invalid. Otherwise this ORDER_BY_clause is the same as that used to order the overall query or subquery. |
|
|
specifies the ordering sequence (ascending or descending). |
|
|
specifies whether returned rows containing null values should appear first or last in the ordering sequence.
|
|
windowing_clause |
|
|
|
These keywords define for each row a "window" (a physical or logical set of rows) used for calculating the function result. The function is then applied to all the rows in the window. The window "slides" through the query result set or partition from top to bottom. You cannot specify this clause unless you have specified the ORDER_BY_clause. |
|
|
Note: The value returned by an analytic function with a logical offset is always deterministic. However, the value returned by an analytic function with a physical offset may produce nondeterministic results unless the ordering expression results in a unique ordering. You may have to specify multiple columns in the ORDER_BY_clause to achieve this unique ordering. |
|
|
lets you specify a start point and end point for the window. The first expression (before |
|
|
If you omit |
|
|
specifies that the window starts at the first row of the partition. This is the start point specification and cannot be used as an end point specification. |
|
|
specifies that the window ends at the last row of the partition. This is the end point specification and cannot be used as a start point specification. |
|
|
As a start point,
As an end point, |
|
|
For |
|
|
If you are defining a logical window defined by an interval of time in numeric format, you may need to use conversion functions. See Also: "NUMTOYMINTERVAL" and "NUMTODSINTERVAL" for information on converting numeric times into interval literals. |
|
|
If you specified
If you specified
|
|
If you omit the windowing_clause entirely, the default is |
|
Analytic functions are commonly used in data warehousing environments. The analytic functions are:
|
See Also:
Oracle8i Data Warehousing Guide for more information on these functions, and for scenarios illustrating their use. |
Object functions manipulate REFs, which are references to objects of specified object types. The object reference functions are:
|
See Also:
Oracle8i Concepts and Oracle8i Application Developer's Guide - Fundamentals for more information about REFs. |
Press "Next" to go to the first SQL function.
|
|
![]() Copyright © 1999 Oracle Corporation. All Rights Reserved. |
|