In Oracle Database, the BIT_TO_NUM()
function converts a bit vector to its equivalent number.
Syntax
The syntax goes like this:
BIN_TO_NUM(expr [, expr ]... )
Where each expr
represents a bit in the bit vector.
The argument/s can be any numeric data type, or any nonnumeric data type that can be implicitly converted to NUMBER
. Each expression must evaluate to 0
or 1
.
Example
Here’s an example:
SELECT BIN_TO_NUM(1)
FROM DUAL;
Result:
1
In this case, the binary value 1 represents the number 1.
Here’s another one:
SELECT BIN_TO_NUM(1, 0)
FROM DUAL;
Result:
2
Now let’s do a much larger number:
SELECT BIN_TO_NUM(1, 0, 1, 1, 1, 0)
FROM DUAL;
Result:
46
Invalid Arguments
Calling BIN_TO_NUM()
with arguments that don’t resolve to a NUMBER
result in an error:
SELECT BIN_TO_NUM('Cat')
FROM DUAL;
Result:
ORA-01722: invalid number
Null Values
If any argument is null
, an “illegal argument” error occurs:
SELECT BIN_TO_NUM(null)
FROM DUAL;
Result:
ORA-01760: illegal argument for function
No Arguments?
Calling BIN_TO_NUM()
without any arguments returns 0
(zero):
SELECT BIN_TO_NUM()
FROM DUAL;
Result:
0