| Oracle8i SQL Reference Release 8.1.5 A67779-01 |
|
Functions are similar to operators in that they manipulate data items and return a result. Functions differ from operators in the format in which they appear with their arguments. This format allows them to operate on zero, one, two, or more arguments:
function(argument, argument, ...)
This chapter describes two types of functions:
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 follow this rule are CONCAT, DECODE, DUMP, 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::=
Table 4-1 lists 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.
| Group | Functions | Description | |
|---|---|---|---|
|
Single-Row Functions |
Single-row functions return a single result row for every row of a queried table or view. Single-row functions can appear in select lists (if the SELECT statement does not contain a GROUP BY clause), 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. |
|||
|
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. |
|||
|
All of the functions listed below return number values. |
|||
|
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. |
|||
|
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. This section lists the SQL conversion functions. |
|||
|
The following single-row functions do not fall into any of the other single-row function categories. |
|||
|
|
|||
|
Object Reference Functions |
Object functions manipulate REFs, which are references to objects of specified object types. For more information about REFs, see Oracle8i Concepts and Oracle8i Application Developer's Guide - Fundamentals. |
||
|
|
|||
|
Aggregate Functions |
Aggregate functions return a single row based on groups of rows, rather than on single rows. Aggregate functions can appear in select lists and HAVING clauses. If you use the GROUP BY clause in a SELECT statement, Oracle divides the rows of a queried table or view into groups. In a query containing a GROUP BY clause, all elements of the select list must be expressions from the GROUP BY clause, expressions containing aggregate functions, or constants. Oracle applies the aggregate functions in the select list 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. For more information on the GROUP BY clause and HAVING clauses, see the "GROUP BY Examples" and the "HAVING" clause. |
||
|
|
Many aggregate functions accept these options:
For example, the DISTINCT average of 1, 1, 1, and 3 is 2; the ALL average is 1.5. If neither option is specified, the default is ALL. |
||
|
|
All aggregate functions except COUNT(*) and GROUPING ignore nulls. You can use the NVL in the argument to an aggregate function to substitute a value for a null. If a query with an aggregate function returns no rows or only rows with nulls for the argument to the aggregate function, the aggregate function returns null. |
||
|
|
|||
|
Syntax |
|
|
Purpose |
Returns the absolute value of n. |
|
Example |
SELECT ABS(-15) "Absolute" FROM DUAL; Absolute ---------- 15 |
|
Syntax |
|
|
Purpose |
Returns average value of n. See also "Aggregate Functions". |
|
Example |
SELECT AVG(sal) "Average" FROM emp; Average ---------- 2077.21429 |
|
Syntax |
|
|
|
Purpose |
Returns a BFILE locator that is associated with a physical LOB binary file on the server's file system. A directory is an alias for a full pathname on the server's file system where the files are actually located, and 'filename' is the name of the file in the server's file system. Neither 'directory' nor 'filename' needs to point to an existing object on the file system at the time you specify BFILENAME. However, you must associate a BFILE value with a physical file before performing subsequent SQL, PL/SQL, DBMS_LOB package, or OCI operations. For more information, see "CREATE DIRECTORY". For more information about LOBs, see Oracle8i Application Developer's Guide - Large Objects (LOBs) and Oracle Call Interface Programmer's Guide. |
|
|
Example |
INSERT INTO file_tbl VALUES (BFILENAME ('lob_dir1', 'image1.gif')); |
|
|
Syntax |
|
|
Purpose |
Returns smallest integer greater than or equal to n. |
|
Example |
SELECT CEIL(15.7) "Ceiling" FROM DUAL; Ceiling ---------- 16 |
|
Syntax |
|
|
Purpose |
Converts a value from CHAR or VARCHAR2 datatype to ROWID datatype. |
|
Example |
SELECT ename FROM emp WHERE ROWID = CHARTOROWID('AAAAfZAABAAACp8AAO'); ENAME ---------- LEWIS |
|
Syntax |
|
|
Purpose |
Returns char1 concatenated with char2. This function is equivalent to the concatenation operator (||). For information on this operator, see "Concatenation Operator". |
|
Example |
This example uses nesting to concatenate three character strings: SELECT CONCAT(CONCAT(ename, ' is a '), job) "Job" FROM emp WHERE empno = 7900; Job ----------------- JAMES is a CLERK |
|
Syntax |
|
|
Purpose |
Returns the cosine of n (an angle expressed in radians). |
|
Example |
SELECT COS(180 * 3.14159265359/180) "Cosine of 180 degrees" FROM DUAL; Cosine of 180 degrees --------------------- -1 |
|
Syntax |
|
|
Purpose |
Returns the hyperbolic cosine of n. |
|
Example |
SELECT COSH(0) "Hyperbolic cosine of 0" FROM DUAL; Hyperbolic cosine of 0 ---------------------- 1 |
|
Syntax |
|
|
Purpose |
Returns the number of rows in the query. If you specify expr, this function returns rows where expr is not null. You can count either all rows, or only distinct values of expr. If you specify the asterisk (*), this function returns all rows, including duplicates and nulls. See also "Aggregate Functions". |
|
Example 1 |
SELECT COUNT(*) "Total" FROM emp; Total ---------- 18 |
|
Example 2 |
SELECT COUNT(job) "Count" FROM emp; Count ---------- 14 |
|
Example 3 |
SELECT COUNT(DISTINCT job) "Jobs" FROM emp; Jobs ---------- 5 |
|
Purpose |
Returns a VARCHAR2 value containing the datatype code, length in bytes, and internal representation of expr. The returned result is always in the database character set. For the datatype corresponding to each code, see Table 2-1. The argument return_fmt specifies the format of the return value and can have any of the values listed below. |
|
|
|
By default, the return value contains no character set information. To retrieve the character set name of expr, specify any of the format values below, plus 1000. For example, a return_fmt of 1008 returns the result in octal, plus provides the character set name of expr. |
|
|
|
8 |
returns result in octal notation. |
|
|
10 |
returns result in decimal notation. |
|
|
16 |
returns result in hexadecimal notation. |
|
|
17 |
returns result as single characters. |
|
|
The arguments start_position and length combine to determine which portion of the internal representation to return. The default is to return the entire internal representation in decimal notation. If expr is null, this function returns 'NULL'. |
|
|
Example 1 |
SELECT DUMP('abc', 1016) FROM DUAL; DUMP('ABC',1016) ------------------------------------------ Typ=96 Len=3 CharacterSet=WE8DEC: 61,62,63 |
|
|
Example 2 |
SELECT DUMP(ename, 8, 3, 2) "OCTAL" FROM emp WHERE ename = 'SCOTT'; OCTAL ---------------------------- Type=1 Len=5: 117,124 |
|
|
Example 3 |
SELECT DUMP(ename, 10, 3, 2) "ASCII" FROM emp WHERE ename = 'SCOTT'; ASCII ---------------------------- Type=1 Len=5: 79,84 |
|
|
Syntax |
|
|
Purpose |
Returns e raised to the nth power, where e = 2.71828183 ... |
|
Example |
SELECT EXP(4) "e to the 4th power" FROM DUAL; e to the 4th power ------------------ 54.59815 |
|
Syntax |
|
|
Purpose |
Returns largest integer equal to or less than n. |
|
Example |
SELECT FLOOR(15.7) "Floor" FROM DUAL; Floor ---------- 15 |
|
Syntax |
|
|
Purpose |
Converts char containing hexadecimal digits to a raw value. |
|
Example |
INSERT INTO graphics (raw_column) SELECT HEXTORAW('7D') FROM DUAL; |
|
Syntax |
|
|
Purpose |
Returns the natural logarithm of n, where n is greater than 0. |
|
Example |
SELECT LN(95) "Natural log of 95" FROM DUAL; Natural log of 95 ----------------- 4.55387689 |
|
Syntax |
|
|
Purpose |
Creates a REF to a row of an object view or a row in an object table whose object identifier is primary key based. For more information about object views, see Oracle8i Application Developer's Guide - Fundamentals. |
|
Example |
CREATE TABLE emp (eno NUMBER, ename VARCHAR2(20), salary NUMBER, PRIMARY KEY (eno, ename)); CREATE TYPE emp_type AS OBJECT (eno NUMBER, ename CHAR(20), salary NUMBER); CREATE VIEW emp_view OF emp_type WITH OBJECT IDENTIFIER (eno, ename) AS SELECT * FROM emp; SELECT MAKE_REF(emp_view, 1, 'jack') FROM DUAL; MAKE_REF(EMP_VIEW,1,'JACK') ------------------------------------------------------ 000067030A0063420D06E06F3C00C1E03400400B40DCB10000001C2 60100010002002900000000000F0600810100140100002A0007000A 8401FE0000001F02C102146A61636B2020202020202020202020202 02020200000000000000000000000000000000000000000 |
|
Syntax |
|
|
Purpose |
Returns maximum value of expr. See also "Aggregate Functions". |
|
Example |
SELECT MAX(sal) "Maximum" FROM emp; Maximum ---------- 5000 |
|
Syntax |
|
|
Purpose |
Returns minimum value of expr. See also "Aggregate Functions". |
|
Example |
SELECT MIN(hiredate) "Earliest" FROM emp; Earliest --------- 17-DEC-80 |
|
Syntax |
|
|
Purpose |
Returns the NLS character set ID number corresponding to NLS character set name, text. The text argument is a run-time VARCHAR2 value. The text value 'CHAR_CS' returns the server's database character set ID number. The text value 'NCHAR_CS' returns the server's national character set ID number. Invalid character set names return null. For a list of character set names, see Oracle8i Reference. |
|
Example I |
SELECT NLS_CHARSET_ID('ja16euc') FROM DUAL; NLS_CHARSET_ID('JA16EUC') ------------------------- 830 |
|
Example 2 |
SELECT NLS_CHARSET_ID('char_cs') FROM DUAL; NLS_CHARSET_ID('CHAR_CS') ------------------------- 2 |
|
Example 3 |
SELECT NLS_CHARSET_ID('nchar_cs') FROM DUAL; NLS_CHARSET_ID('NCHAR_CS') -------------------------- 2 |
|
Syntax |
|