BIN Function in SQLThe 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 FunctionIn 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 functionExample 1: This example returns the binary representation of the specified number: Output:
Example 2: This example returns the binary representation of the specified number: Output:
Example 3: This example returns the binary representation of 8: Output:
Example 4: This example returns the binary representation of 255: Output:
Example 5: This example returns the binary representation of NULL: SELECT BIN(NULL) AS Binary_of_NULL; Output:
Example 6: This example returns the binary representation of NULL: Output:
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:
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:
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:
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:
Next TopicMAX Function in SQL |