SQL Function Types
SQL Function Types
Number Functions
CEIL MOD
FLOOR
ROUND - Number Function
TRUNC
Character Functions
CHR LTRIM TRANSLATE
CONCAT REPLACE TRIM
INITCAP RPAD UCASE See UPPER
LCASE See LOWER ROUND - Date Function UPPER
LOWER SUBSTR USER
LPAD SUBSTRB
Character Functions Returning Number Values
ASCII INSTR LENGTHB
BIT_LENGTH (See LENGTH) INSTRB OCTET_LENGTH
(See LENGTH)
CHAR_LENGTH LENGTH POSITION
(See LENGTH)
Date Functions
ADD_MONTHS HOUR SYSDATE
CURDATE LAST_DAY TIMESTAMPADD
CURRENT_DATE MINUTE TIMESTAMPDIFF
CURRENT_TIME MONTH TRUNC
CURRENT_TIMESTAMP MONTHNAME WEEK
CURTIME MONTHS_BETWEEN YEAR
DAYNAME NEXT_DAY
DAYOFMONTH NOW
DAYOFWEEK ROUND - Date Function
DAYOFYEAR SECOND
Conversion Functions
CAST TO_CHAR TO_DATE
CONVERT TO_NUMBER
Other Functions
CASE GREATEST LOCATE
DATABASE IFNULL NVL
(See CASE and NVL)
DECODE INTERVAL SUBSTR
EXTRACT LEAST USER
Grouping Functions
AVG STDDEV
COUNT SUM
MAX VARIANCE
MIN
SQL functions are used exclusively with SQL commands within SQL statements.
There are two general types of SQL functions: single row (or scalar) functions and
aggregate functions. These two types differ in the number of database rows on which
they act. A single row function returns a value based on a single row in a query,
whereas an aggregate function returns a value based on all the rows in a query.
Single row SQL functions can appear in select lists (except in SELECT statements
that contain a GROUP BY clause) and WHERE clauses.
Aggregate functions are the "set" functions: AVG, MIN, MAX, SUM, and COUNT.
You must provide them with an alias that can be used by the GROUP BY function.
Most functions have a SQL form and an ODBC form that can differ slightly in
functionality. Either form can be used in embedded SQL.
3.2.1 Number Functions
3.2.2 Character Functions
Single row character functions accept character input and can return both character
and number values.
3.2.4 Date Functions
Date functions operate on values of the DATE datatype. All date functions return a
value of the DATE datatype, except the MONTHS_BETWEEN function which returns a
number.
3.2.5 Conversion Functions
Conversion functions convert a value from one datatype to another. Generally, the
form of the function name follows the convention datatype TO datatype. The first
datatype is the input datatype; the last datatype is the output datatype.
Syntax
Purpose
Argument and Description
Examples
Usage Notes
ODBC Functionality (where relevant)
3.3.1 ADD_MONTHS
Syntax
ADD_MONTHS(d, n)
Purpose
Example
SELECT TO_CHAR(ADD_MONTHS(hiredate,1)),'DD-MM-YYYY' "Next month"FROM emp WHERE
ename = 'SMITH'
3.3.2 ASCII
Syntax
ASCII(char)
Purpose
Returns the decimal representation in the database character set of the first byte
of char. If your database character set is 7-bit ASCII, this function returns an ASCII
value.
Example
SELECT ASCII('Q') FROM DUAL;
3.3.3 AVG
Syntax
AVG([DISTINCT | ALL] n)
Purpose
Returns the average value of a column n.
Example 1
SELECT AVG(SAL) FROM EMP;
Example 2
SELECT {FN AVG (SAL)} FROM EMP;
Example 3
SELECT AVG (DISTINCT DEPTNO) FROM EMP;
Example 4
SELECT AVG (ALL DEPTNO) FROM EMP;
ODBC Function
{FN AVG ([DISTINCT | ALL] n)}
Syntax
CASE
WHEN condition 1 THEN result 1
WHEN condition 2 THEN result 2
...
WHEN condition n THEN result n
ELSE result x
END,
Purpose
Argument Description
WHEN Begins a condition clause.
Condition Specifies the condition.
THEN Begins a result clause.
Result Specifies the result of the associated condition.
ELSE An optional clause specifying the result of any value not described in a condition
clause.
END Terminates the case statement.
Usage Notes
The CASE function specifies conditions and results for a select or update statement.
You can use the CASE function to search for data based on specific conditions or to
update values based on a condition.
Example
SELECT CASE JOB
WHEN 'PRESIDENT' THEN 'The Honorable'
WHEN 'MANAGER' THEN 'The Esteemed'
ELSE 'The good'
END,
ENAME
FROM EMP;
Returns the following result:
CASEJOBWHEN'PRESI ENAME
----------------- ----------
The Honorable KING
The Esteemed BLAKE
The Esteemed CLARK
The Esteemed JONES
The good MARTIN
The good ALLEN
The good TURNER
The good JAMES
The good WARD
The good FORD
The good SMITH
The good SCOTT
The good ADAMS
The good MILLER
14 rows selected.
3.3.5 CAST
Syntax
SELECT CAST ( <source_operand > AS <data_type > ) FROM DUAL;
Purpose
Argument Description
<source_operand> a value expression or NULL.
<data_type> the type of target.
Usage Notes
The following table displays the conversion results of source operands to datatypes:
Figure 3-1
Results of Source Operands and Datatypes
Result Definitions
EN = exact number D = date
C = fixed or variable length character TS = timestamp
VC = variable length character DT = date-time
T = time V = valid
YM = year-month interval R = valid with restrictions
AN = approximate numeric X = invalid
FC = fixed length character
Example 1
SELECT CAST('0' AS INTEGER) FROM DUAL;
Example 2
SELECT CAST(0 AS REAL) FROM DUAL;
Example 3
SELECT CAST(1E0 AS NUMERIC(12, 2)) FROM DUAL;
Example 4
SELECT CAST(CURRENT_TIMESTAMP AS VARCHAR(30)) FROM DUAL;
3.3.6 CEIL
Syntax
CEIL (n)
Purpose
Example
SELECT CEIL(15.7) "Ceiling" FROM DUAL;
3.3.7 CHR
Syntax
CHR (n)
Purpose
Returns the character with the binary equivalent to n in the database character set.
Example
SELECT CHR(68)||CHR(79)||CHR(71) "Dog" FROM DUAL;
3.3.8 CONCAT
Syntax
CONCAT(char1, char2)
or
CHAR1 || CHAR2
Purpose
Example
ODBC Function
{FN CONCAT (char1, char2)}
3.3.9 CONVERT
Syntax
{ fn CONVERT(value_exp, data_type) }
Purpose
Usage Notes
The following includes a list of common character sets:
Table 3-4 Common Character Sets Used with the CONVERT Function
Common Character
Sets
US7ASCII WE8ISO8859P1
WE8DEC HP West European Laserjet 8-bit character set
WE8HP DEC French 7-bit character set
F7DEC IBM West European EBCDIC Code Page 500
WE8EBCDIC500 IBM PC Code Page 850 ISO 8859-1 West European 8-bit
character set
WE8PC850 ISO 8859-1 West European 8-bit character set
Example
SELECT {fn CONVERT('Groß', 'US7ASCII') }
"Conversion" FROM DUAL;
3.3.10 COUNT
Syntax
COUNT([* | [DISTINCT | ALL] expr})
Purpose
Example 1
SELECT COUNT(*) "Total" FROM emp;
Example 2
SELECT COUNT(job) "Count" FROM emp;
Example 3
SELECT COUNT(DISTINCT job) "Jobs" FROM emp;
Example 4
SELECT COUNT (ALL JOB) FROM EMP;
3.3.11 CURDATE
Syntax
{ fn CURDATE ( <value_expression > ) }
Purpose
Usage Notes
If you specify expr (expression), 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.
Example 1
SELECT {fn CURDATE()} FROM DUAL;
Example 2
SELECT {fn WEEK({fn CURDATE()})} FROM DUAL;
3.3.12 CURRENT_DATE
Syntax
CURRENT_DATE
Purpose
Example
SELECT CURRENT_DATE FROM DUAL;
ODBC Function
{fn CURDATE()}
3.3.13 CURRENT_TIME
Syntax
CURRENT_TIME
Purpose
Example
SELECT CURRENT_TIME FROM DUAL;
ODBC Function
{fn CURTIME()}
3.3.14 CURRENT_TIMESTAMP
Syntax
CURRENT_TIMESTAMP
Purpose
Returns the current local date and local time as a timestamp value but only displays
the current local date by default. You can view current local time information by
using CURRENT_TIMESTAMP as a value of the TO_CHAR function and by including a time
format. See Example 2.
Example 1
SELECT CURRENT_TIMESTAMP FROM DUAL;
Example 2
SELECT TO_CHAR (CURRENT_TIMESTAMP, 'HH24:MM:SS, Day, Month, DD, YYYY')FROM
DUAL;
ODBC Function
{fn CURTIME()}
3.3.15 CURTIME
Syntax
{ fn CURTIME ( <value_expression > ) }
Purpose
Example 1
SELECT {fn CURTIME()} FROM DUAL;
Example 2
SELECT {fn HOUR({fn CURTIME()})} FROM DUAL;
Syntax
{ fn DATABASE () }
Purpose
Specifies the name of the database. If you are using ODBC, the DATABASE function
returns the name of the current default database file without the .ODB extension.
Usage Notes
Example
The following example returns a result for users connected to the default database.
SELECT {fn DATABASE () } FROM DUAL;
3.3.17 DAYNAME
Syntax
{ fn DAYNAME (date_expr) }
Purpose
Example
SELECT {fn dayname({fn curdate()})} from dual;
Returns the current day of the week as a string.
3.3.18 DAYOFMONTH
Syntax
{ fn DAYOFMONTH ( <value_expression > ) }
Purpose
Argument Description
<value_expression> Date on which the day of the month is computed. The result is between 1
and 31, where 1 represents the first day of the month.
Example 1
SELECT {fn DAYOFMONTH ({fn CURDATE()})} FROM DUAL;
Example 2
SELECT {fn DAYOFMONTH('1997-07-16')} "DayOfMonth" FROM DUAL;
3.3.19 DAYOFWEEK
Syntax
{ fn DAYOFWEEK ( <value_expression > ) }
Purpose
Returns the day of the week as an integer.
Argument Description
<value_expression> Date on which the day of the week is computed. The result is between 1
and 7, where 1 represents Sunday.
Example 1
SELECT {fn DAYOFWEEK ({fn CURDATE()})} FROM DUAL;
Example 2
SELECT {fn DAYOFWEEK('1997-07-16')} "DayOfWeek" FROM DUAL;
3.3.20 DAYOFYEAR
Syntax
{ fn DAYOFYEAR ( <value_expression > ) }
Purpose
Argument Description
<value_expression> A date on which the day of the year is computed. The result is between 1
and 366.
Example 1
SELECT {fn DAYOFYEAR ({fn CURDATE()})} FROM DUAL;
Example 2
SELECT {fn DAYOFYEAR('1997-07-16')} "DAYOFYEAR" FROM DUAL;
3.3.21 DECODE
Syntax
DECODE (expr, search, result [, search, result...] [,default])
Purpose
Search for an expression's values and then evaluate them in terms of a specified result.
Usage Notes
To evaluate an expression, Oracle Lite compares the expression to each search value
one by one. If the expression is equal to a search, Oracle Lite returns the
corresponding result. If no match is found, Oracle Lite returns default, or, if default is
omitted, returns null. If the expression and search contain character data, Oracle Lite
compares them using nonpadded comparison semantics.
The search, result, and default values can be derived from expressions. Oracle Lite
evaluates each search value only before comparing it to the expression, rather than
evaluating all search values before comparing any of them with the expression.
Consequently, Oracle never evaluates a search if a previous search is equal to the
expression.
Oracle Lite automatically converts the expression and each search value to the
datatype of the first search value before making comparisons. Oracle Lite
automatically converts the return value to the same datatype as the first result. If the
first result has the datatype CHAR or if the first result is null, then Oracle Lite
converts the return value to the datatype VARCHAR2.
Example 1
The following expression decodes the DEPTNO column in the DEPT table. If
DEPTNO is 10, the expression evaluates to 'ACCOUNTING';if DEPTNO is 20, it
evaluates to 'RESEARCH'; and so on. If DEPTNO is not 10, 20, 30, or 40, the
expression returns 'NONE'.
DECODE (deptno, 10, 'ACCOUNTING',
20, 'RESEARCH',
30, 'SALES',
40, 'OPERATIONS',
'NONE')
Example 2
Syntax
EXTRACT (extract-field FROM extract source)
Purpose
Example 1
SELECT EXTRACT (DAY FROM '06-15-1966') FROM DUAL;
Example 2
SELECT EXTRACT (YEAR FROM {FN CURDATE()}) FROM DUAL;
3.3.23 FLOOR
Syntax
FLOOR (n)
Purpose
Returns largest integer equal to or less than n.
Example
SELECT FLOOR(15.7) "Floor" FROM DUAL;
3.3.24 GREATEST
Syntax
GREATEST(expr [,expr] ...)
Purpose
Returns the greatest of the list of exprs (expressions). All exprs after the first are
implicitly converted to the datatype of the first exprs before the comparison. Oracle
Lite compares the exprs using non-padded comparison semantics. Character
comparison is based on the value of the character in the database character set. One
character is greater than another if it has a higher value. If the value returned by this
function is character data, its datatype is always VARCHAR2.
Example
SELECT GREATEST('HARRY','HARRIOT','HAROLD') "GREATEST" FROM DUAL;
3.3.25 HOUR
Syntax
HOUR (time_exp)
Purpose
Returns the hour as an integer value in the range of 0-23.
Example 1
SELECT {FN HOUR ('14:03:01')} FROM DUAL;
Example 2
SELECT {fn HOUR({fn CURTIME()})} FROM DUAL;
3.3.26 INITCAP
Syntax
INITCAP(char)
Purpose
Returns char, with the first letter of each word in uppercase, all other letters in
lowercase. Words are delimited by white space or characters that are not
alphanumeric.
Example
SELECT INITCAP('the soap') "Capitals" FROM DUAL;
3.3.27 INSTR
Syntax
INSTR(char1, char2, [, n [, m ]])
Purpose
Usage Notes
If n is negative, INSTR counts and searches backward from the end of char1. The
value of m must be positive. The default values of both n and m are 1, meaning that
INSTR begins searching at the first character of char1 for the first occurrence
of char2. The return value is relative to the beginning of char1, regardless of the value
of n, and is expressed in characters. If the search is unsuccessful (if char2 does not
appear m times after the nth character of char1), the return value is 0. For additional
information, see the syntax for the "POSITION" function.
Example
SELECT INSTR('CORPORATE FLOOR','OR',3,2) "Instring" FROM DUAL;
3.3.28 INSTRB
Syntax
INSTRB(char1, char2, [, n [, m ]])
Purpose
Example
SELECT INSTRB('CORPORATE FLOOR','OR',5,2) "Instring in bytes" FROM DUAL;
3.3.29 INTERVAL
Syntax
INTERVAL (datetime values)
Purpose
Subtracts one datetime from another and generates the result. When you add or
subtract one interval from another, the result is always another interval. You can
multiply or divide an interval by a numeric constant.
Example 1
SELECT CURRENT_DATE - INTERVAL '8' MONTH FROM DUAL;
Example 2
SELECT TO_CHAR (INTERVAL '6' DAY * 3) FROM DUAL;
Syntax
LAST_DAY(d)
Purpose
Returns a date that represents the last day of the month in which date d occurs.
Usage Notes
You can use this function to determine how many days are left in the current month.
Example 1
SELECT LAST_DAY (SYSDATE) FROM DUAL;
Example 2
SELECT SYSDATE,
LAST_DAY(SYSDATE) "Last",
LAST_DAY(SYSDATE) - SYSDATE "Days Left"
FROM DUAL;
3.3.31 LEAST
Syntax
LEAST(expr [,expr] ...)
Purpose
Returns the least of the list of exprs (expressions). All exprs after the first are
implicitly converted to the datatype of the first exprs before the comparison. Oracle
Lite compares the exprs using non-padded comparison semantics. Character
comparison is based on the value of the character in the database character set. One
character is less than another if it has a lower value. If the value returned by this
function is character data, its datatype is always VARCHAR2.
Example
SELECT LEAST('HARRY','HARRIOT','HAROLD') "LEAST" FROM DUAL;
3.3.32 LENGTH
Syntax
LENGTH (char)
{fn LENGTH(char)}
BIT_LENGTH (char)
CHAR_LENGTH (char)
OCTET_LENGTH (char)
Purpose
Returns the length in characters of the string argument char. If char has the datatype
CHAR, the length includes all trailing blanks. If char is null, it returns null.
Usage Notes
3.3.33 LENGTHB
Syntax
LENGTHB(char)
{fn LENGTHB(char)}
Purpose
Returns the length in bytes of the string argument char. If char is null, it returns null.
For a single-byte database character set, LENGTHB is equivalent to "LENGTH".
Example
SELECT LENGTHB('CANDIDE') "Length in bytes" FROM DUAL;
3.3.34 LOCATE
Syntax
LOCATE (string_exp1, string_exp2[,start])
Purpose
Returns the starting position of the first occurrence of string_exp1 within the first
character position of string_exp2. You can use the start value to specify a search
location other than the first character position of string_exp2.
Example 1
The following example selects the starting position of the character 'R' in the string
expression 'TURNER' for every row of the EMP table.
SELECT {FN LOCATE ('R', 'TURNER')} FROM EMP ENAME;
14 rows selected.
Example 2
The following example selects the starting position of the character 'R' in the string
expression 'TURNER' and starts its search at the fourth character in 'TURNER'. The example
displays the results found for every occurrence of 'TURNER' in every row of
the EMP table.
SELECT {FN LOCATE ('R', 'TURNER',4)} FROM EMP ENAME;
14 rows selected.
3.3.35 LOWER
Syntax
LOWER(char)
Purpose
Returns a string argument char, with all its letters in lowercase. The return value has
the same datatype as char, either CHAR or VARCHAR2.
Example
SELECT LOWER('LOWER') FROM DUAL;
ODBC Function
{fn LCASE (char)}
3.3.36 LPAD
Syntax
LPAD(char1,n [,char2])
Purpose
Example
SELECT LPAD('Page1',15,'*.') "LPAD example" FROM DUAL;
3.3.37 LTRIM
Syntax
LTRIM(char [, set])
Purpose
Returns the string argument char, with its left-most characters removed up to the first
character which is not in the string argument set, which defaults to (a single space).
Example
SELECT LTRIM ('xyxXxyLAST WORD','xy') "LTRIM example" FROM DUAL;
ODBC Function
{fn LTRIM (char) } (trims leading blanks)
3.3.38 MAX
Syntax
MAX([DISTINCT | ALL] expr)
Purpose
Example
SELECT MAX(SAL) FROM EMP;
3.3.39 MIN
Syntax
MIN([DISTINCT | ALL] expr)
Purpose
Example
SELECT MIN(SAL), MAX(SAL) FROM EMP;
3.3.40 MINUTE
Syntax
MINUTE (time_exp)
Purpose
Example 2
SELECT {fn MINUTE({fn CURTIME()})} FROM DUAL;
3.3.41 MOD
Syntax
MOD (m,n)
Purpose
Example
SELECT MOD (26,11) "ABLOMOV" FROM DUAL;
3.3.42 MONTH
Syntax
MONTH (date_exp)
Purpose
Example 1
SELECT {FN MONTH ('06-15-1966')} FROM DUAL;
Example 2
SELECT {fn MONTH({fn CURDATE()})} FROM DUAL;
3.3.43 MONTHNAME
Syntax
{ fn MONTHNAME (date_exp) }
Purpose
Example
select {fn monthname({fn curdate()})} from dual;
3.3.44 MONTHS_BETWEEN
Syntax
MONTHS_BETWEEN(d1, d2 )
Purpose
Example
SELECT MONTHS_BETWEEN(
TO_DATE('02-02-1995','MM-DD-YYYY'),
TO_DATE('01-01-1995','MM-DD-YYYY') ) "Months"
FROM DUAL;
3.3.45 NEXT_DAY
Syntax
NEXT_DAY(d, char)
Purpose
Returns the date of the first weekday named by char that is later than the date d. The
argument char must be a day of the week in your session's date language. The return
value has the same hours, minutes, and seconds component as the argument d.
Example
SELECT NEXT_DAY('15-MAR-92','TUESDAY') "NEXT DAY" FROM DUAL;
3.3.46 NOW
Syntax
NOW
Purpose
Returns the current local date and local time as a timestamp value but only displays
the current local date by default. You can view current local time information by
using NOW as a value of the TO_CHAR function and by including a time format. See
Example 2.
Example 1
SELECT {FN NOW()} FROM DUAL;
Example 2
SELECT TO_CHAR ({fn NOW ('YYYY, Month, DD, HH24:MM:SS')}) FROM DUAL;
3.3.47 NVL
Syntax
NVL(expr1, expr2)
Purpose
If expr1 is null, returns expr2; if expr1 is not null, returns expr1. The
arguments expr1 and expr2 must be of the same datatype.
Example 1
SELECT ename, NVL(TO_CHAR(COMM),'NOT APPLICABLE') "COMMISSION"
FROM emp
WHERE deptno = 30;
6 rows selected.
Example 2
SELECT {fn IFNULL(Emp.Ename, 'Unknown')},
NVL (Emp.comm, 0) FROM EMP;
14 rows selected.
Example 3
SELECT sal+NVL(comm, 0) FROM EMP;
Returns the following result:
SAL+NVL(COMM
------------
5000
2850
2450
2975
2650
1900
1500
950
1750
3000
800
3000
1100
1300
14 rows selected.
ODBC Function
{fn IFNULL (expr1, expr2)}
3.3.48 POSITION
Syntax
POSITION ( <substring_value_expression>
IN <value_expression> )
Argument Description
<value_expression> a source string to search in.
<substring_value_expression> a sub-string to search for.
<start_len_cnt> the starting position for the search
Purpose
Usage Notes
If the length of <substring_value_expression> is 0, the result is null. If
<substring_value_expression> occurs in <value_expression>, the result is the position
of the first character of <substring_value_expression>. Otherwise, the result is 0. If
<start_len_cnt> is omitted, the function starts the search from position 1. For
additional information, see the INSTR and INSTRB functions.
Example
SELECT POSITION ('CAT' IN 'CATCH') FROM DUAL;
ODBC Function
{fn LOCATE ( <substring_value_expression> ,
<value_expression>[, <start_len_cnt> ] ) }
3.3.49 QUARTER
Syntax
{ fn QUARTER ( <value_expression> ) }
Argument Description
<value_expression> A date on which the quarter is computed. The result is between 1 and 4,
where 1 represents January 1 through March 31.
Purpose
Example
SELECT {fn QUARTER ({fn CURDATE()})} FROM DUAL;
3.3.50 REPLACE
Syntax
REPLACE(char, search_string [, replacement_string])
Purpose
Usage Notes
Example
SELECT REPLACE('JACK and JUE','J','BL') "Changes" FROM DUAL;
Syntax
ROUND(d [,fmt])
The following table lists the format models to be used with the ROUND (and
TRUNC) date function, and the units to which it rounds dates. The default model,
'DD', returns the date rounded to the day with a time of midnight.
Table 3-10 The Format Models with the ROUND Date Function
YEAR, SYEAR,
YYY, YY, Y
IYYY, IYY, IY, I ISO Year
Q Quarter (rounds up in the sixteenth day of the second month of the
quarter)
MONTH, MON, MM, Month (rounds up on the sixteenth day)
RM
WW Same day of the week as the first day of the year
IW Same day of the week as the first day of the ISO year
W Same day of the week as the first day of the month
DDD, DD, J Day
DAY, DY, D Starting day of the week.
HH, HH12, HH24 Hour
MI Minute
Purpose
Example
SELECT ROUND(TO_DATE('27-OCT-92'),'YEAR')
"FIRST OF THE YEAR" FROM DUAL;
Syntax
ROUND(n [,m ])
Purpose
Example 1
SELECT ROUND (54.339, 2) FROM DUAL;
3.3.53 RPAD
Syntax
RPAD(char1,n [,char2 ])
Purpose
The argument n is the total length of the return value as it is displayed on your
terminal screen. In most character sets, this is also the number of characters in the
return value. However, in some multi-byte character sets, the display length of a
character string can differ from the number of characters in the string.
Example
SELECT RPAD('ename',12,'ab') "RPAD example"
FROM emp
WHERE ename = 'TURNER';
3.3.54 RTRIM
Syntax
RTRIM(char [,set])
Purpose
Returns the string argument char, with its right-most characters removed following
the last character which is not in the string argument set. This defaults to ' ' (a single
space).
Example 1
SELECT RTRIM ('TURNERyxXxy', 'xy') "RTRIM example" FROM DUAL;
Example 2
SELECT {fn RTRIM ('TURNERyxXxy', 'xy')} FROM DUAL;
ODBC Function
{fn RTRIM (char)} (trims leading blanks)
3.3.55 SECOND
Syntax
SECOND (time_exp)
Purpose
Example 1
SELECT {FN SECOND ('14:03:01')} FROM DUAL;
Example 2
SELECT {fn SECOND({fn CURTIME()})} FROM DUAL;
3.3.56 STDDEV
Syntax
STDDEV([DISTINCT|ALL] x)
Purpose
Returns the standard deviation of x, a number. Oracle Lite calculates the standard
deviation as the square root of the variance defined for the VARIANCE group
function.
Example
SELECT STDDEV(sal) "Deviation" FROM emp;
Returns the following result:
Deviation
---------
1182.5032
3.3.57 SUBSTR
Syntax
SUBSTR(char, m [, n ])
Purpose
Usage Notes
If m is positive, SUBSTR counts from the beginning of char to find the first character.
If m is negative, SUBSTR counts backwards from the end of char. The value m cannot
be 0. If n is omitted, SUBSTR returns all characters to the end of char. The
value ncannot be less than 1.
Example
SELECT SUBSTR('ABCDEFG',3,4) "Subs" FROM DUAL;
3.3.58 SUBSTRB
Syntax
SUBSTRB(char, m [,n])
Purpose
Returns a portion of the string argument char, beginning with the byte at
position m and n bytes long. The same as SUBSTR, except that the
arguments m and n specify bytes rather than characters. For a single-byte database
character set, SUBSTRB is equivalent to SUBSTR.
Example
SELECT SUBSTRB('ABCDEFG',5,4) "Substring with bytes" FROM DUAL;
3.3.59 SUM
Syntax
SUM([DISTINCT | ALL] n)
Purpose
Example
SELECT deptno, SUM(sal) TotalSalary FROM emp GROUP BY deptno;
3.3.60 SYSDATE
Syntax
SYSDATE
Purpose
Returns the current date and time. Requires no arguments.
Usage Notes
You cannot use this function in the condition of the Oracle Lite DATE type column.
You can only use the time in a TIME column, and both date and time in a
TIMESTAMP column.
Example
SELECT TO_CHAR(SYSDATE, 'MM-DD-YYYY HH24:MI:SS') NOW FROM DUAL;
3.3.61 TIMESTAMPADD
Syntax
{fn TIMESTAMPADD (<interval>, <value_exp1 >, <value_exp2 >)}
<value_exp1 > + <value_exp2 >
Argument Description
<interval> Specifies the unit of the second operand, <value_exp1>. The following
keywords are valid values for intervals:
SQL_TSI_FRAC_SECOND
SQL_TSI_SECOND
SQL_TSI_MINUTE
SQL_TSI_HOUR
SQL_TSI_DAY
Argument Description
SQL_TSI_WEEK
SQL_TSI_MONTH
SQL_TSI_QUARTER
SQL_TSI_YEAR
<value_exp1> an integer
<value_exp2> a timestamp
<value_expression> an operand
Purpose
Example
The following example adds one day to the current timestamp for 1999-04-13:
SELECT {fn TIMESTAMPADD (SQL_TSI_DAY, 1, {fn NOW()})} FROM DUAL;
3.3.62 TIMESTAMPDIFF
Syntax
{fn TIMESTAMPDIFF (<interval>, <value_exp1 >, <value_exp2 >)}
<value_expression > - <value_expression >
Argument Description
<interval> specifies the unit of the second operand, <value_exp1>. The following
Argument Description
keywords are valid values for intervals:
SQL_TSI_FRAC_SECOND
SQL_TSI_SECOND
SQL_TSI_MINUTE
SQL_TSI_HOUR
SQL_TSI_DAY
SQL_TSI_WEEK
SQL_TSI_MONTH
SQL_TSI_QUARTER
SQL_TSI_YEAR
<value_exp1> an integer
<value_exp2> a timestamp
<value_expression> an operand
Purpose
Calculates the difference between two timestamp values using a specified interval.
Example 1
SELECT {fn TIMESTAMPDIFF (SQL_TSI_DAY, {fn CURDATE()}, '1998-12-09')} FROM
DUAL;
Example 2
SELECT ENAME, {fn TIMESTAMPDIFF (SQL_TSI_YEAR, {fn CURDATE()},HIREDATE)} FROM
EMP;
Returns the following result:
ENAME {FNTIMESTAMPDIFF(SQL_TSI_YEA
---------- ----------------------------
KING -17
BLAKE -17
CLARK -17
JONES -18
MARTIN -17
ALLEN -18
TURNER -17
JAMES -17
WARD -18
FORD -17
SMITH -18
SCOTT -16
ADAMS -16
MILLER -17
14 rows selected.
3.3.63 TO_CHAR
Purpose
Converts a date or number to a value of the VARCHAR2 datatype, using the optional
format fmt.
Argument Description
d date column or SYSDATE
fmt format string
n number column or literal
Usage Notes
You can specify a default date format for all databases on your computer by setting
the NLS_DATE_FORMAT parameter in the POLITE.INI file. See the Oracle Lite
User's Guide for more information on setting the NLS_DATE_FORMAT parameter
in the POLITE.INI file.
Example
SELECT TO_CHAR (SYSDATE, 'Day, Month, DD, YYYY')"TO_CHAR example" FROM DUAL;
3.3.64 TO_DATE
Syntax
TO_DATE(char [, fmt ])
Purpose
Example
SELECT TO_DATE('January 26, 1996, 12:38 A.M.', 'Month dd YYYY HH:MI A.M.')
FROM DUAL;
3.3.65 TO_NUMBER
Syntax
TO_NUMBER(char [, fmt ])
Purpose
Usage Notes
Example
The following example updates the salary of an employee named Blake according to
the value specified in the TO_NUMBER function. In this example, you first view
Blake's salary. Then, update Blake's salary and view it again.
SELECT * FROM EMP WHERE ENAME = 'BLAKE';
3.3.66 TRANSLATE
Syntax
TRANSLATE(char, from, to)
Purpose
Usage Notes
You cannot use an empty string for to to remove from the return value all characters
in from. TRANSLATE interprets the empty string as null, and if this function has a
null argument, it returns null.
Example
SELECT TRANSLATE('2KRW229', '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ',
'9999999999XXXXXXXXXXXXXXXXXXXXXXXXXX') "Licence" FROM DUAL;
3.3.67 TRIM
Syntax
TRIM( [[<trim_spec >] char ]
FROM ] string )
Argument Description
<trim_spec> a specification: LEADING, TRAILING, or BOTH
char a single character
string the target string to be trimmed
Purpose
Removes leading and/or trailing blanks (or other characters) from a string.
Example
SELECT TRIM ('OLD' FROM 'OLDMAN') FROM DUAL;
3.3.68 TRUNC
Syntax with Numeric Arguments
TRUNC(n [, m])
Returns the date d with its time portion truncated to the time unit specified by the
format model fmt. If you omit fmt, then d is truncated to the nearest day.
Usage Notes
The following table lists the format models to be used with the TRUNC (and
ROUND) date function, and the units to which it rounds dates. The default model,
'DD', returns the date rounded to the day with a time of midnight.
YEAR, SYEAR,
YYY, YY, Y
IYYY, IYY, IY, I ISO Year
Q Quarter (rounds up in the sixteenth day of the second month of the
quarter)
MONTH, MON, MM, Month (rounds up on the sixteenth day)
RM
WW Same day of the week as the first day of the year
Format Model Rounding Unit
IW Same day of the week as the first day of the ISO year
W Same day of the week as the first day of the month
DDD, DD, J Day
DAY, DY, D Starting day of the week
HH, HH12, HH24 Hour
MI Minute
Example 1
SELECT TRUNC(TO_DATE('27-OCT-92', 'DD-MON-YY'), 'YEAR') "First Of The Year"
FROM DUAL;
Example 2
SELECT TRUNC(15.79,1) "Truncate" FROM DUAL;
Example 3
SELECT TRUNC(15.79,-1) "Truncate" FROM DUAL;
3.3.69 UPPER
Syntax
UPPER(char)
Purpose
Returns the string argument char with all its letters converted to uppercase. The return
value has the same datatype as char.
Example
SELECT UPPER('Carol') FROM DUAL;
ODBC Function
{fn UCASE (char)}
3.3.70 USER
Syntax
USER
Purpose
Example 1
SELECT USER "User" FROM DUAL;
Example 2
SELECT {fn USER()} FROM DUAL;
ODBC Function
{ fn USER()}
3.3.71 VARIANCE
Syntax
VARIANCE([DISTINCT|ALL] x)
Purpose
Returns variance of x, a number. Oracle Lite calculates the variance of x using this
formula:
n is the number of elements in the set x. If n is 1, the variance is defined to be 0.
Example
SELECT VARIANCE(sal) "Variance" FROM emp;
3.3.72 WEEK
Syntax
{ fn WEEK ( <value_expression> ) }
Purpose
Example 1
SELECT {fn WEEK({fn CURDATE()})} FROM DUAL;
Example2
SELECT {fn week('1999-06-15')} FROM DUAL;
3.3.73 YEAR
Syntax
YEAR (date_exp)
Purpose
Example 1
SELECT {FN YEAR ('06-15-1966')} FROM DUAL;
Example 2
SELECT {fn YEAR({fn CURDATE()})} FROM DUAL;
Returns the following result:
{FNYEAR({FNCURDATE()})}
-----------------------
1999