Oracle TO_BINARY_FLOAT() Function
Oracle TO_BINARY_FLOAT() is a built-in function that converts the given expression to a binary float.
Oracle TO_BINARY_FLOAT() Syntax
Here is the syntax for the Oracle TO_BINARY_FLOAT() function:
TO_BINARY_FLOAT(expr [ DEFAULT return_value ON CONVERSION ERROR ]
[, fmt [, 'nlsparam' ] ])
Parameters
expr-
Required. It can be any expression that evaluates to a character string of type
CHAR,VARCHAR2,NCHAR, orNVARCHAR2, a numeric value of typeNUMBER,BINARY_FLOAT, orBINARY_DOUBLE, or null. You cannot use floating-point format elements (F,f,D, ord) in the character stringexpr. DEFAULT return_value ON CONVERSION ERROR-
Optional. It allows you to specify a value to return if a conversion error occurs.
return_valuecan be an expression or a bind variable, and it must evaluate to a character string of typeCHAR,VARCHAR2,NCHAR, orNVARCHAR2, a numeric value of typeNUMBER,BINARY_FLOAT, orBINARY_DOUBLE, or null. The function convertsreturn_valueto aBINARY_DOUBLEin the same way as it convertsexprtoBINARY_DOUBLE. Ifreturn_valuecannot be converted toBINARY_DOUBLE, the function returns an error. fmt-
Optional. A format string.
'nlsparam'-
Optional. You can use this parameter to set the parameter using the
'NLS_DATE_LANGUAGE = language'form, wherelanguageis the language name.
Return Value
The Oracle TO_BINARY_FLOAT() function returns a binary float.
If expr is BINARY_DOUBLE, the function returns expr. If expr evaluates to null, the function returns null. Otherwise, the function converts expr to a BINARY_DOUBLE value.
If expr or return_value evaluates to the following character strings, the function converts them as follows:
- The case-insensitive string “
INF” is converted to positive infinity. - The case-insensitive string “
-INF” is converted to negative infinity. - The case-insensitive string “
NaN” is converted toNaN(not a number).
Note:
- Conversion from character strings or
NUMBERtoBINARY_DOUBLEmay not be precise becauseNUMBERand character types represent numeric values using decimal precision, whereasBINARY_DOUBLEuses binary precision. - Conversion from
BINARY_FLOATtoBINARY_DOUBLEis precise.
Oracle TO_BINARY_FLOAT() Examples
Here are a few examples that demonstrate the usage of the Oracle TO_BINARY_FLOAT() function.
Basic Usage
The following statement demonstrates the basic usage of the Oracle TO_BINARY_FLOAT() function:
SELECT
TO_BINARY_FLOAT(1)
FROM dual;
输出:
TO_BINARY_FLOAT(1)
_____________________
1.0INF and -INF
The Oracle TO_BINARY_FLOAT() function converts the case-insensitive strings “INF” and “-INF” to positive infinity and negative infinity, respectively.
SELECT
TO_BINARY_FLOAT('INF') "INF",
TO_BINARY_FLOAT('-INF') "-INF"
FROM dual;
输出:
INF -INF
___________ ____________
Infinity -InfinityNaN
The Oracle TO_BINARY_FLOAT() function converts the case-insensitive string “NaN” to NaN (not a number).
SELECT
TO_BINARY_FLOAT('NaN') "NaN",
TO_BINARY_FLOAT('nan') "nan",
TO_BINARY_FLOAT('NAN') "NAN"
FROM dual;
输出:
NaN nan NAN
______ ______ ______
NaN NaN NaNNULL Argument
If any argument is NULL, TO_BINARY_FLOAT() returns NULL.
SET NULL 'NULL';
SELECT
TO_BINARY_FLOAT(NULL)
FROM dual;
输出:
TO_BINARY_FLOAT(NULL)
________________________
NULLIn this example, we use the statement SET NULL 'NULL'; to display NULL values as the string 'NULL'.
Conclusion
Oracle TO_BINARY_FLOAT() is a built-in function that converts the given expression to a binary float.