BIN Function in SQL

The BIN is a SQL function which converts the given decimal number to its binary equivalent. This function return NULL, if the NULL is passed in the function.

Syntax of BIN Function

In the BIN syntax, we have to pass that decimal number whose binary equivalent we want to find.

In the Structured Query Language, we can also use the BIN function with the column of the table as shown in the following block:

In this syntax, we have to define the name and column of that table on which we want to perform the BIN function.

Examples of BIN function

Example 1: This example returns the binary representation of the specified number:

Output:

Binary_of_101
1100101

Example 2: This example returns the binary representation of the specified number:

Output:

Binary_of_2
10

Example 3: This example returns the binary representation of 8:

Output:

Binary_of_8
1000

Example 4: This example returns the binary representation of 255:

Output:

Binary_of_255
11111111

Example 5: This example returns the binary representation of NULL:

SELECT BIN(NULL) AS Binary_of_NULL;

Output:

Binary_of_NUL
NULL

Example 6: This example returns the binary representation of NULL:

Output:

21222324252627282930
10101101101011111000110011101011011111001110111110

Example 7: This example uses the BIN function with the SQL table.

In this example, we will create the new table through which we will perform the BIN function on the columns of the table:

The following shows the syntax to create the new table in SQL:

The following CREATE statement creates the Product_Details table for storing the price and quantity of products:

The following multiple INSERT queries insert the records of products with their selling and purchasing price into the Product_Details table:

The following SELECT statement displays the inserted records of the above Product_Details table:


Product_IDProduct_NameProduct_QuantityPurchasing_PriceSelling_PriceRelease_DateProduct_Rating
104P110.250945NULL2022-04-30NULL
202P415.50045752022-01-285
103P218.25025NULL2022-02-184
111P725.2505152021-12-259
210P615.50050702021-10-15NULL
212P819.7501102502022-01-284
112P1010.2505508352022-04-11NULL

Query 1: The following SELECT query uses the BIN function with the Product_Quantity column of the above Product_Details table:

This query shows the binary representation of product id of each product.

Output:

Product_IDBinary_of_Product_ID
1041101000
20211001010
1031100111
1111101111
21011010010
21211010100
1121110000

Query 2: The following SELECT query uses the BIN function with the and Purchasing_Price and Selling_Price column of the above Product_Details table:

This query shows the binary representation of purchasing and selling price of each product.

Output:

Purchasing_PriceBinary_of_PurchasingPriceSelling_PriceBinary_of_SellingPrice
9451110110001NULLNULL
45101101751001011
2511001NULLNULL
5101151111
50110010701000110
110110111025011111010
55010001001108351101000011

Query 3: The following SELECT query uses the BIN function with the Product_Rating column of the above Product_Details table:

This query shows the binary representation of rating of each product from the above table.

Output:

Product_RatingBinary_of_productrating
NULLNULL
5101
4100
91001
NULLNULL
4100
NULLNULL





Latest Courses