GREATEST
Purpose
Returns the largest value in the list of input arguments.
Syntax
where:
Syntax element … |
Specifies … |
TD_SYSFNLIB |
the name of the database where the function is located. |
numeric_value |
a numeric value. |
string_value |
a string value. |
ANSI Compliance
This is a Teradata extension to the ANSI SQL:2011 standard.
Invocation
GREATEST is an embedded services system function. For information on activating and invoking embedded services functions, see “Embedded Services System Functions” on page 24.
Argument Types and Rules
Expressions passed to this function must have the following data types:
BYTEINT, SMALLINT, INTEGER, BIGINT, DECIMAL/NUMERIC, FLOAT/REAL/DOUBLE PRECISION, NUMBER, CHAR, or VARCHAR
All of the input arguments must be the same data type or else the types must be compatible.
Result Type
If the input arguments are numeric types, the function determines which argument has the highest precedence, converts the other arguments to that data type, and returns that data type. For details about the order of precedence, see “Compatible Types” in SQL External Routine Programming.
If the data type is DECIMAL/NUMERIC and the precision and scale of the input arguments are different, the precision and scale of the return type is set to achieve the maximum precision possible. For example, if the input arguments are DECIMAL(6,3), DECIMAL(7,4), and DECIMAL(8,7), the return type would need 3 digits to the left of the decimal point and 7 digits to the right of the decimal point to avoid any reduction in precision. In this case, the return data type is set to DECIMAL(10,7).
In cases where it is not possible to maintain the maximum precision, the data is rounded according to the DBS Control Record RoundHalfWayMagUp field. For example, if the input arguments are DECIMAL(32, 8) and DECIMAL(30, 28), the return type is DECIMAL(38,14). This allows for 24 digits to the left of the decimal point (required for the DECIMAL(32,8) argument), and 14 digits to the right of the decimal point. If the DECIMAL(30,28) input argument is the greatest value, it is rounded to 14 places to the right of the decimal point.
If the data type is fixed point NUMBER and the precision is less than or equal to 38, the precision and scale of the return type are calculated with the same method used for DECIMAL/NUMERIC. However, if the precision is greater than 38, the return type is changed to NUMBER(*) to avoid loss of accuracy. If the data type is floating point NUMBER, the return type is NUMBER(*).
If the input arguments are character types, the function converts the 2nd through 10th arguments to the data type of the 1st argument and returns the type as VARCHAR in the character set of the 1st argument.
For information on the default data type formats, see SQL Data Types and Literals.
Usage Notes
If the arguments are character types, the string comparison uses non-padded comparison semantics. Character comparison is binary and based on the numerical codes of the characters. The string is treated as a sequence of bytes for the comparison rather than character by character.
If any of the input arguments is NULL, the function returns NULL.
Example
The following query returns the result 13.
SELECT GREATEST(13, 6);
Example
In the following query, if the input arguments have data types of DECIMAL(4,2) and DECIMAL(5,4), the return data type is DECIMAL(6,4) and the result value is 13.1200.
SELECT GREATEST(13.12, 6.1234);
Example
The following query returns the result 'apples'.
SELECT GREATEST('apples', 'alpha');