The GREATEST and LEAST functions accept a list of two or more
value expressions (all of which must be of comparable types) and
return the greatest value from the list for the GREATEST function
and the least value from the list for the LEAST function.
The value expressions specified can be column references,
subselects, function calls, literal values, and other complex
value expressions.
The data type of the resulting expression is a common data type
to which all value expressions in the list can be converted. For
example, LEAST(10, 10.3, 123E100) results in a DOUBLE PRECISION
result because at least one literal is DOUBLE PRECISION.
If the result data type resolves to a fixed CHARACTER string,
then GREATEST and LEAST return a CHARACTER VARYING (also known as
VARCHAR) string with the maximum length.
The NULL keyword can appear in the list but is ignored. However,
not all value expressions can be specified as NULL. That is, a
non-NULL value expression must be in the list so that the data
type for the expression can be determined.
The GREATEST and LEAST functions can result in NULL only if at
run time all value expressions result in NULL.
If DATE VMS literals are used as an argument to the GREATEST or
LEAST function, the date string must be prefixed with the type
DATE VMS so that SQL will accept it as a DATE VMS literal and not
a string literal. See the following examples.
Examples: Using the GREATEST and LEAST functions
Example 1 Using CHARACTER Versus DATE VMS Literals
SQL> -- Different results are returned by the LEAST function (and
SQL> -- the GREATEST function) if the parameters are treated as
SQL> -- CHARACTER or DATE VMS literals. This is because it is
SQL> -- the data types of the source expressions that determine the
SQL> -- the result data type of the LEAST (and GREATEST) functions.
SQL> select LEAST ('1-APR-2001', '10-JAN-2000'),
cont> LEAST (DATE VMS'1-APR-2001', DATE VMS'10-JAN-2000')
cont> from rdb$database;
1-APR-2001 10-JAN-2000 00:00:00.00
1 row selected
Example 2 finds the least value for the birthday of employees
from two employees tables.
Example 2 Using the LEAST Function
SQL> SELECT LEAST (M.BIRTHDAY, E.BIRTHDAY, :SUPPLIED_DATE)
cont> FROM EMPLOYEES E, JOB_HISTORY JH, DEPARTMENTS D, EMPLOYEES M
cont> WHERE E.EMPLOYEE_ID = JH.EMPLOYEE_ID AND
cont> ...