BIN() function in SQL is used when working with binary numbers, networking concepts, bitwise operations, access control systems and low-level computer programming. It helps convert integers into binary strings that represent how computers internally store numeric values.
The BIN() function in SQL converts an integer into its corresponding binary form. It returns a string that has binary digits i.e. 0 and 1.
number: It is an integer value that needs to be converted to binary form.
The BIN() function returns the binary equivalent of the input decimal number as a string.
Let us create a table named ComputerDevices that will store information about different networking and computing devices commonly used in organizations.
The table consists of the following fields:
DeviceID: It is a unique number assigned to each device.
DeviceName: It will store the name of the device.
IPAddressLastOctet: It will store the last octet of an IP address which is often used in networking calculations.
AccessLevel: It will represent the access permission level assigned to the device.
PortNumber: It will store the network port number used by the device for communication.
SQL Query
Now we will insert records into the ComputerDevices table.
SQL Query
Let us first retrieve the records from the table before applying the BIN() function.
SQL Query:
ComputerDevices Table:
| DeviceID | DeviceName | IPAddressLastOctet | AccessLevel | PortNumber |
|---|---|---|---|---|
| 1 | Router | 25 | 7 | 80 |
| 2 | Switch | 18 | 5 | 22 |
| 3 | Firewall | 45 | 15 | 443 |
| 4 | Server | 88 | 31 | 8080 |
| 5 | Printer | 10 | 3 | 9100 |
| 6 | Access Point | 30 | 6 | 53 |
| 7 | Workstation | 64 | 12 | 3389 |
| 8 | Database Server | 100 | 28 | 3306 |
| 9 | Proxy Server | 50 | 10 | 3128 |
The table above contains device-related details for various computer devices. We will apply the BIN() function to different numeric columns to transform their decimal values into binary representations.
In this example, we will explore how to utilize the BIN() function to convert an integer into its binary equivalent.
SQL Query:
Output:
| Binary_Value |
|---|
| 11001 |
Explanation:
We have here utilized the BIN() function to convert the integer 25 into its binary form. The function returned the binary string 11001 corresponds to the integer 25 in binary form.
Here we will apply the BIN() function to the result of an arithmetic expression. It demonstrates that BIN() can transform calculated values as well as direct numbers.
SQL Query:
Output:
| BinaryResult |
|---|
| 1000000 |
Explanation:
The expression 50 + 14 is evaluated first which yields the value 64. The BIN() function then converts the number 64 into its binary form which is 1000000.
We will be utilizing the BIN() function to convert the decimal value 0 into its binary representation.
SQL Query:
Output:
| BinaryValue |
|---|
| 0 |
Explanation:
In the above query, we have used the BIN() function to convert the value 0 into binary format. Since the binary representation of zero is also zero so the function returns 0.
We will here be discussing how the BIN() function deals with NULL value is passed as an argument.
SQL Query:
Output:
| BinaryValue |
|---|
| NULL |
Explanation:
We have here utilized BIN() function to convert a NULL value into binary format. It simply returns NULL.
In this example we have utilized the BIN() function to convert a negative value into its binary representation.
SQL Query:
Output:
| BinaryValue |
|---|
| 1111111111111111111111111111111111111111111111111111111111111011 |
Explanation:
We have here used the BIN() function to convert the negative number -5 into binary format. The function internally uses the storage of data type to convert the value into binary. For this reason, we get a long binary string as the output.
We will utilize the BIN() function on the DeviceID column. Device ID is a stored as decimal integers but the BIN() function can convert them into binary values to show how computers internally represent numeric identifiers.
SQL Query:
Output:
| DeviceID | DeviceName | BinaryDeviceID |
|---|---|---|
| 1 | Router | 1 |
| 2 | Switch | 10 |
| 3 | Firewall | 11 |
| 4 | Server | 100 |
| 5 | Printer | 101 |
| 6 | Access Point | 110 |
| 7 | Workstation | 111 |
| 8 | Database Server | 1000 |
| 9 | Proxy Server | 1001 |
Explanation:
We have used the BIN() function in the above example to convert each DeviceID into its binary value. As a result, we get the binary representation of the DeviceID.
In this example below, we will be using BIN() function on the IPAddressLastOctet column. It can be particularly useful in networking and IP address calculation tasks.
SQL Query:
Output:
| DeviceName | IPAddressLastOctet | BinaryIP |
|---|---|---|
| Router | 25 | 11001 |
| Switch | 18 | 10010 |
| Firewall | 45 | 101101 |
| Server | 88 | 1011000 |
| Printer | 10 | 1010 |
| Access Point | 30 | 11110 |
| Workstation | 64 | 1000000 |
| Database Server | 100 | 1100100 |
| Proxy Server | 50 | 110010 |
Explanation:
We have applied BIN() function on the IPAddressLastOctet column. This column stores the last Octet of the IPv4 addresses. We can use BIN() function to convert these decimal values into their binary equivalent.
In this example we will convert the AccessLevel column into binary format with the help of BIN() function. Access levels are commonly used in security systems and permission management. Binary values are often used internally to represent permission flags and access controls.
SQL Query:
Output:
| DeviceName | AccessLevel | BinaryAccessLevel |
|---|---|---|
| Router | 7 | 111 |
| Switch | 5 | 101 |
| Firewall | 15 | 1111 |
| Server | 31 | 11111 |
| Printer | 3 | 11 |
| Access Point | 6 | 110 |
| Workstation | 12 | 1100 |
| Database Server | 28 | 11100 |
| Proxy Server | 10 | 1010 |
Explanation:
The BIN() function in this demonstration converts the AccessLevel values into binary form. It is useful in understanding how access permissions are internally stored.
Let us combine the BIN() function with the WHERE clause together. The WHERE clause will first filter the relevant records and BIN() will convert the filtered decimal values into binary.
SQL Query:
Output:
| DeviceName | IPAddressLastOctet | BinaryIP |
|---|---|---|
| Server | 88 | 1011000 |
| Workstation | 64 | 1000000 |
| Database Server | 100 | 1100100 |
Explanation:
In this example, we have combined BIN() function with WHERE clause to first filter the IPAddressLastOctet values that are larger than 50. After filtering the relevant decimal numbers, we use the BIN() function to convert these values into their binary equivalent.
Here we will be using BIN() function on the PortNumber column and then sort the results according to the original decimal values of ports.
SQL Query:
Output:
| DeviceName | PortNumber | BinaryPort |
|---|---|---|
| Switch | 22 | 10110 |
| Access Point | 53 | 110101 |
| Router | 80 | 1010000 |
| Firewall | 443 | 110111011 |
| Proxy Server | 3128 | 110000111000 |
| Database Server | 3306 | 110011101010 |
| Workstation | 3389 | 110100111101 |
| Server | 8080 | 1111110010000 |
| Printer | 9100 | 10001110001100 |
Explanation:
We have utilized BIN() function on the PortNumber column to convert the port numbers into binary. The resulting binary numbers can be used to determine the binary representation of the port numbers in networking. We have also utilized the ORDER BY clause to sort the results according to the original decimal values stored in the PortNumber column.
We request you to subscribe our newsletter for upcoming updates.