MariaDB BIN() Function

In MariaDB, BIN() is a built-in string function that returns the string representation of the binary value of a given number.

MariaDB BIN() Syntax

Here is the syntax of the MariaDB BIN() function:

BIN(num)

The MariaDB BIN() function has the same result as CONV(num, 10, 2).

Parameters

num

Required. Requires a number represented in binary. You can provide a large number, eg BIGINT.

If a parameter is missing, MariaDB will report an error: ERROR 1582 (42000): Incorrect parameter count in the call to native function 'BIN'.

Return value

The MariaDB BIN() function returns a string representing of the binary value of the given number.

If the num argument is of non-numeric type, the BIN() function will try to convert it to a number first before returning the binary representation of the number.

If the argument num is NULL, the BIN() function will return NULL.

MariaDB BIN() Examples

Basic example

This statement uses the MariaDB BIN() function that returns the string representation of the binary value of the number 8:

SELECT BIN(8);

Output:

+--------+
| BIN(8) |
+--------+
| 1000   |
+--------+

This is the same as CONV(8, 10, 2), as follows:

SELECT CONV(8, 10 ,2);

Output:

+----------------+
| CONV(123,10,2) |
+----------------+
| 1111011        |
+----------------+

String parameter

You can provide a string type parameter to the MariaDB BIN() function, and MariaDB will try to convert it to a number first and then return its corresponding binary string representation.

This statement uses the MariaDB BIN() function that returns the string representation of the binary value of '8':

SELECT
  BIN('8'),
  BIN('8A'),
  BIN('A8');

Output:

+----------+-----------+-----------+
| BIN('8') | BIN('8A') | BIN('A8') |
+----------+-----------+-----------+
| 1000     | 1000      | 0         |
+----------+-----------+-----------+

In this example, MariaDB tries to convert the string parameter to a number before proceeding to the next calculation:

  • Convert '8' to 8, and then output the string representation of the binary value of 8.
  • Convert '8A'to 8, and then output the string representation of the binary value of 8.
  • 'A8' cannot be converted to a number, so returns 0.

Float Parameters

If you provide a float argument to the MariaDB BIN() function, MariaDB will truncate the float to an integer and return the binary value of the integer.

SELECT BIN(8.12);

Output:

+-----------+
| BIN(8.12) |
+-----------+
| 1000      |
+-----------+

In this example, the MariaDB BIN() function truncates 8.12 to 8 and returns a string representation of the binary value of 8.

Other Examples

Here is an example of all the usages of the MariaDB BIN() function:

SELECT
    BIN(2),
    BIN(5),
    BIN(8),
    BIN(8.5),
    BIN('8A'),
    BIN('A8'),
    BIN('A'),
    BIN(NULL)\G

Output:

*************************** 1\. row ***************************
   BIN(2): 10
   BIN(5): 101
   BIN(8): 1000
 BIN(8.5): 1000
BIN('8A'): 1000
BIN('A8'): 0
 BIN('A'): 0
BIN(NULL): NULL

Conclusion

The MariaDB BIN() function returns a string representation of the binary value of a given number.