At least I forgot all about LEAST, a very simple Oracle SQL function.
You want to select the smallest value from a list of expressions, you use LEAST:
LEAST(expr [, expr ]…)
Some examples:
select least (33, 23, 10, 7) as least_value
from dual
LEAST_VALUE
----------------------
7
1 rows selected
select least ('hoola', 'hoop', 'hoopla') as least_value
from dual
LEAST_VALUE
-----------
hoola
1 rows selected
select least ('.', '2', 3, 'a') as least_value
from dual
LEAST_VALUE
-----------
.
1 rows selected
When comparing values, Oracle uses nonpadded comparison semantics. With nonpadded semantics, Oracle compares two values character by character up to the first character that differs. The value with the greater character in that position is considered greater.
If two values of different length are identical up to the end of the shorter one, then the longer value is considered greater.
If two values of equal length have no differing characters, then the values are considered equal.
Another example:
select least (1, '2', 3, 'a') as least_value
from dual
SQL Error: ORA-01722: invalid number
Oops! an error. Why? well, because Oracle implicitly converts the datatype of all expressions in the list to the datatype of the first expression before the comparison. The datatype of the first expression in the example above is number. When Oracle tries to convert ‘a’ to number it fails and returns ORA-01722.
Possibly related:
- Oracle REF CURSOR and ColdFusion
- Cool SQL function: EXTRACT
- Previously Undocumented LNNVL SQL Function Buggy
- SYS_CONTEXT in Oracle
- Cool SQL Analytic Function: RATIO_TO_REPORT
Tagged function, sql | Post a Comment


















Home > About This Post
This entry was posted by Eddie Awad on Tuesday, March 14th, 2006, at 10:26 pm, and was filed in Oracle.
Subscribe to the
RSS 2.0 feed for all comments to this post.
Post a Comment