How to use the BIT_LENGTH() function in SQL
Overview
The BIT_LENGTH() function calculates and returns the number of bits in a given string.
Syntax
BIT_LENGTH(string)
Parameter
string: This represents the string whose bit length will be returned.
Example
The following code shows how to use the BIT_LENGTH() function in SQL.
CREATE TABLE Product (id int,product_name varchar(50),price varchar(50),product_id varchar (20));-- Insert dataINSERT INTO ProductVALUES (101,'T-shirt','$100','oo-01-345');INSERT INTO ProductVALUES (102,'Hand Bag','$65','oo-01-238');INSERT INTO ProductVALUES (103,'Pen','$2','oo-01-103');INSERT INTO ProductVALUES (104,'Cereal','$30','oo-01-775');INSERT INTO ProductVALUES (105,'Wooden spoon','$25','oo-01-788');INSERT INTO ProductVALUES (106,'cloth clips','$15','oo-01-924');INSERT INTO ProductVALUES (108,'Zara Perfume','$120','oo-01-245');-- QuerySELECT product_id,product_name, BIT_LENGTH(product_name) AS btlen_product_nameFROM Product;
Explanation
In the code above:
- Line 1–7: We create a table called
Productwith the columnsid,product_name,price,andbook_id. - Line 11–21: We add data into the
Booktable. - Line 24–26: We return the bit length number of the given string
product_namecolumn using theBIT_LENGTH()function.