SQL Tutorial SQL Advanced SQL Database SQL References

MySQL BIN() Function



The MySQL BIN() function converts a decimal number to a binary number and returns the result as a string value.

The BIN() function is equivalent to using the CONV() function with CONV(number, 10, 2) syntax.

Syntax

BIN(number)

Parameters

number Required. Specify the number to convert to a binary representation.

Return Value

Returns the binary representation of a decimal number.

Example 1:

The example below shows the usage of BIN() function.

mysql> SELECT BIN(1);
Result: '1'

mysql> SELECT BIN(2);
Result: '10'

mysql> SELECT BIN(10);
Result: '1010'

mysql> SELECT BIN(128);
Result: '10000000'

mysql> SELECT BIN(200);
Result: '11001000'

mysql> SELECT BIN(1024);
Result: '10000000000'

Example 2:

Consider a database table called Sample with the following records:

Datax
Data 110
Data 220
Data 330
Data 440
Data 550

The statement given below can be used to get the binary representation of values of column x.

SELECT *, BIN(x) AS BIN_Value FROM Sample;

This will produce the result as shown below:

DataxBIN_Value
Data 1101010
Data 22010100
Data 33011110
Data 440101000
Data 550110010

❮ MySQL Functions