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:
Data | x |
---|---|
Data 1 | 10 |
Data 2 | 20 |
Data 3 | 30 |
Data 4 | 40 |
Data 5 | 50 |
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:
Data | x | BIN_Value |
---|---|---|
Data 1 | 10 | 1010 |
Data 2 | 20 | 10100 |
Data 3 | 30 | 11110 |
Data 4 | 40 | 101000 |
Data 5 | 50 | 110010 |
❮ MySQL Functions