Oracle BIN_TO_NUM() Function

Oracle BIN_TO_NUM() is a built-in function that returns the number converted from the bit vector specified by the arguments.

Oracle BIN_TO_NUM() Syntax

Here is the syntax of the Oracle BIN_TO_NUM() function:

BIN_TO_NUM(expr [, expr ]... )

Parameters

expr

Required. Each argument represents a bit in the bit vector. Each argument can be any numeric data type or any non-numeric data type that can be implicitly converted to NUMBER. Each argument must evaluate to either 0 or 1.

Return Value

The Oracle BIN_TO_NUM() function returns the number converted from the bit vector specified by the arguments, which is a NUMBER type value.

If any of the arguments are NULL, BIN_TO_NUM() returns NULL.

Oracle BIN_TO_NUM() Examples

Here are some examples that demonstrate the usage of the Oracle BIN_TO_NUM() function.

Basic Usage

The following example shows how to convert binary values to numbers:

SELECT
    BIN_TO_NUM(0) "0",
    BIN_TO_NUM(1) "1",
    BIN_TO_NUM(1, 0) "10",
    BIN_TO_NUM(1, 1) "11",
    BIN_TO_NUM(1, 0, 1, 0) "1010",
    BIN_TO_NUM(1, 1, 1, 1) "1111"
FROM dual;

输出:

   0    1    10    11    1010    1111
____ ____ _____ _____ _______ _______
   0    1     2     3      10      15

In this example:

  • Binary value 0 is converted to decimal 0.
  • Binary value 1 is converted to decimal 1.
  • Binary value 10 is converted to decimal 10.
  • Binary value 11 is converted to decimal 11.
  • Binary value 1010 is converted to decimal 1010.
  • Binary value 1111 is converted to decimal 1111.

NULL Arguments

You cannot provide a NULL argument, or else Oracle will report an error:

SET NULL 'NULL';
SELECT
    BIN_TO_NUM(NULL)
FROM dual;

输出:

SQL Error: ORA-01760: illegal argument for function
01760. 00000 -  "illegal argument for function"
*Cause:
*Action:

In this example, we use the SET NULL 'NULL'; statement to display NULL values as the string 'NULL'.

Conclusion

Oracle BIN_TO_NUM() is a built-in function that returns the number converted from the bit vector specified by the arguments.