BIN
BIN is a MySQL function that is used to convert a decimal number to its binary equivalent. It takes a numerical value as an input and returns its binary representation stored as a string.
BIN(N)
- n: This is an integral number that will be converted to binary. For example, BIN(12) will return ‘1100’, representing the binary form of the number 12.
Example
SELECT BIN(12);
Output
'1100'
Explanation
The BIN()
function in MySQL is used to convert a decimal number to binary. In the provided example, the decimal number 12 is converted to binary, resulting in ‘1100’.
BIN( integer_expression )
- integer_expression: This is the positive integer, an integer or an integer expression, that you want to convert to a binary number in SQL Server.
Example
SELECT BIN(12);
Output
1100
Explanation
The BIN() function is a mathematical function in SQL Server. It returns a binary representation of a given decimal number. In the provided example, the decimal number 12 is converted to a binary string. The binary equivalent of the decimal number 12 is ‘1100’.
BIN_TO_NUM(bin_to_num1, bin_to_num2, …, bin_to_numN)
- bin_to_num1: This is the first binary expression that is input into the BIN_TO_NUM function. It is either 0 or 1.
- bin_to_num2: This is the second binary expression in the sequence, following bin_to_num1. Like the first parameter, it can be either 0 or 1.
- …, bin_to_numn: This designation signifies that the function can accept any number of binary values. Each value must be either 0 or 1. These values together form the binary number that is converted to a number by the function.
Oracle SQL does not support BIN function natively. Instead, it provides the TO_NUMBER
function with the parameter ‘2’ to achieve a similar result. Thus, we are going to use this function in our example.
Example
SELECT TO_NUMBER('1101', '2') FROM dual;
Output
13
Explanation
In this example, we use the TO_NUMBER
function to convert the binary string ‘1101’ into its decimal equivalent. The function takes two arguments: the string to be converted and the base of the number system (in this case, 2 for binary). The result returned is 13, as the binary number 1101 is equivalent to the decimal number 13.