MySQL BIN() Function

The BIN() function in MySQL returns a binary (base-2) representation of a given number as a string.


Syntax

BIN(number)

Parameters

Parameter Description
number Required. The number to convert to binary.

Definition and Usage

  • The BIN() function converts a number to its binary representation in the form of a string.
  • The binary output will represent the number in base-2 format (using only 0s and 1s).

Return Values

  • Returns a string that represents the binary value of the given number.

Usage Examples

Example 1: Return binary representation of 15

SELECT BIN(15);

Output:

BIN(15)
1111

Explanation: The binary representation of the decimal number 15 is 1111.

Example 2: Return binary representation of 111

SELECT BIN(111);

Output:

BIN(111)
1101111

Explanation: The binary representation of the decimal number 111 is 1101111.

Example 3: Return binary representation of 8

SELECT BIN(8);

Output:

BIN(8)
1000

Explanation: The binary representation of the decimal number 8 is 1000.


Technical Details

  • Works in: From MySQL 4.0.
  • Return Type: String, representing the binary format of the number.

Key Notes

  • The BIN() function can be used for both integer and floating-point numbers.
  • The output will always be in binary format, and it is represented as a string of 0s and 1s.

Was this article helpful?