Miscellaneous single-row Functions
The following single-row functions do not fall into any of the other single-row function categories:
Decodepurpose
DECODE
Comparesexpr
To eachsearch
Value one by one. Ifexpr
Is equal tosearch
, Then oracle returns the correspondingresult
. If no match is found, then oracle returnsdefault
. Ifdefault
Is omitted, then oracle returns NULL.
Ifexpr
Andsearch
Contain character data, then oracle compares them using nonpadded comparison semantics.expr
,search
, Andresult
Can be any of the datatypesCHAR
,VARCHAR2
,NCHAR
, OrNVARCHAR2
. The string returned isVARCHAR2
Datatype and is in the same character set as the firstresult
Parameter.
Thesearch
,result
, Anddefault
Values can be derived from expressions. Oracle evaluates eachsearch
Value only before comparing itexpr
, Rather than evaluating allsearch
Values before comparing any of themexpr
. Consequently, Oracle never evaluatessearch
If a previoussearch
Is equalexpr
.
Oracle automatically convertsexpr
And eachsearch
Value to the datatype of the firstsearch
Value before comparing. Oracle automatically converts the return value to the same datatype as the firstresult
. If the firstresult
Has the datatypeCHAR
Or if the firstresult
Is null, then oracle converts the return value to the datatypeVARCHAR2
.
InDECODE
Function, Oracle considers two nulls to be equivalent. Ifexpr
Is null, then oracle returnsresult
Of the firstsearch
That is also null.
The maximum number of components inDECODE
Function, includingexpr
,searches
,results
, Anddefault
, Is 255.
Examples
This example decodes the valuewarehouse_id
. Ifwarehouse_id
Is 1, then the function returns'Southlake
'; Ifwarehouse_id
Is 2, then it returns'San Francisco
'; And so forth. Ifwarehouse_id
Is not 1, 2, 3, or 4, then the function returns'Non-domestic
'.
SELECT product_id,
DECODE (warehouse_id, 1, 'Southlake',
2, 'San Francisco',
3, 'New Jersey',
4, 'Seattle',
'Non-domestic')
"Location of inventory" FROM inventories
WHERE product_id < 1775;
Nvlpurpose
NVL
Lets you replace a null (blank) with a string in the results of a query. Ifexpr1
Is null, thenNVL
Returnsexpr2
. Ifexpr1
Is not null, thenNVL
Returnsexpr1
. The argumentsexpr1
Andexpr2
Can have any datatype. If their datatypes are different, then oracle convertsexpr2
To the datatypeexpr1
Before comparing them.
The datatype of the return value is always the same as the datatypeexpr1
, Unlessexpr1
Is character data, in which case the return value's datatype isVARCHAR2
And is in the character setexpr1
.
Examples
The following example returns a list of employee names and commissions, substituting "Not applicable" if the employee has es no Commission:
SELECT last_name, NVL(TO_CHAR(commission_pct), 'Not Applicable')
"COMMISSION" FROM employees
WHERE last_name LIKE 'B%'
ORDER BY last_name;
LAST_NAME COMMISSION
------------------------- ----------------------------------------
Baer Not Applicable
Baida Not Applicable
Banda .1
Bates .15
Bell Not Applicable
Bernstein .25
Bissot Not Applicable
Bloom .2
Bull Not Applicable
Greatestpurpose
GREATEST
Returns the greatest of the listexprs
. Allexpr
S after the first are implicitly converted to the datatype of the firstexpr
Before the comparison. Oracle comparesexpr
S using nonpadded 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 character set value. if the value returned by this function is character data, then its datatype is alwaysVARCHAR2
.
Examples
The following statement selects the string with the greatest value:
SELECT GREATEST ('HARRY', 'HARRIOT', 'HAROLD')
"Greatest" FROM DUAL;
Greatest
--------
HARRY
Leastpurpose
LEAST
Returns the least of the listexpr
S. Allexpr
S after the first are implicitly converted to the datatype of the firstexpr
Before the comparison. Oracle comparesexpr
S using nonpadded comparison semantics. If the value returned by this function is character data, then its datatype is alwaysVARCHAR2
.
Examples
The following statement is an example of usingLEAST
Function:
SELECT LEAST('HARRY','HARRIOT','HAROLD') "LEAST"
FROM DUAL;
LEAST
------
HAROLD