How BIN() Works in MariaDB

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

You provide the longlong number when you call the function.

Syntax

The syntax goes like this:

BIN(N)

Where N is the longlong number.

Example

Here’s a simple example:

SELECT BIN(123);

Result:

+----------+
| BIN(123) |
+----------+
| 1111011  |
+----------+

This is the same as CONV(123, 10, 2). Here it is alongside that function:

SELECT 
    BIN(123), 
    CONV(123,10,2);

Result:

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

Using a float Value

If the argument is a float, it’s truncated.

Example:

SELECT BIN(123.456);

Result:

+--------------+
| BIN(123.456) |
+--------------+
| 1111011      |
+--------------+

Wrong Argument Type

Passing the wrong argument type returns 0.

Example:

SELECT BIN('Homer');

Result:

+--------------+
| BIN('Homer') |
+--------------+
| 0            |
+--------------+

Null Arguments

Passing null returns null:

SELECT BIN(null);

Result:

+-----------+
| BIN(null) |
+-----------+
| NULL      |
+-----------+

Missing Argument

Calling BIN() without passing an argument results in an error:

SELECT BIN();

Result:

ERROR 1582 (42000): Incorrect parameter count in the call to native function 'BIN'