BIT_LENGTH Function in SQL
The BIT_LENGTH string function of Structured Query Language returns the length of the string in bits.
Syntax of BIT_LENGTH String Function
Syntax1: This syntax uses the BIT_LENGTH function with the column name of the SQL table:
In the syntax, we have to specify the name of that column on which we want to perform the BIT_LENGTH string function for finding the length of the string in bits.
Syntax2: This syntax uses the BIT_LENGTH function with the string:
Examples of BIT_LENGTH String function
Example 1: The following SELECT query shows the length of the word in bits:
Output:
BIT_LENGTH_word |
---|
80 |
Example 2: The following SELECT query shows the length of the given string in bits:
Output:
BIT_LENGTH_string |
---|
224 |
Example 3: The following SELECT query shows the length of the given sentence in bits:
Output:
BIT_LENGTH_Sentence |
---|
264 |
Example 4: The following SELECT query shows the length of the given space in bits:
Output:
BIT_LENGTH_space |
---|
8 |
Example 5: The following SELECT query shows the length of the NULL word in bits:
Output:
Length |
---|
NULL |
Example 6: This example uses the BIT_LENGTH function with the table in Structured Query Language.
Here, we are going to create a new SQL table on which we want to execute the BIT_LENGTH function.
The below CREATE TABLE statement is the syntax for creating the new SQL table in the database:
The following CREATE statement creates the Employee_Grade table:
The below INSERT queries insert the records of Employees with Grades and Remarks in the Employee_Grade table:
The following SELECT statement displays the inserted records of the above Employee_Grade table:
Employee_ID | First_Name | Last_Name | First_City | Second_City | New_City | Attendance_Remarks | Work_Remarks | Grade |
---|---|---|---|---|---|---|---|---|
10 | Ramesh | Sharma | Lucknow | Aurangabad | Ghaziabad | 88 | 95 | A2 |
02 | Yadu | Sharma | Aurangabad | Ghaziabad | Ghaziabad | 95 | 82 | A1 |
07 | Vijay | Ramna | Noida | Ghaziabad | Lucknow | 91 | 95 | A1 |
04 | Bhanu | Rangopalr | Ghaziabad | Noida | Lucknow | 85 | 82 | A2 |
11 | Harry | Roy | Noida | Kanpur | Ghaziabad | 95 | 97 | A1 |
16 | Akash | Ramna | Ghaziabad | Meerut | Aurangabad | 95 | 90 | B1 |
19 | Ram | Ramna | Lucknow | Ghaziabad | Aurangabad | 89 | 95 | A2 |
Query 1: The following SELECT query uses the BIT_LENGTH function with the First_Name column of the above Employee_Grade table:
This statement shows the length of the first name of each employee in bits.
First_Name | BIT_LENGTH_FirstName |
---|---|
Ramesh | 48 |
Yadu | 32 |
Vijay | 40 |
Bhanu | 40 |
Harry | 40 |
Akash | 40 |
Ram | 24 |
Query 2: The following SELECT query uses the BIT_LENGTH function with the Last_Name column of the above Employee_Grade table:
This statement shows the length of the last name of each employee in bits.
Output:
Last_Name | BIT_LENGTH_LastName |
---|---|
Sharma | 48 |
Sharma | 48 |
Ramna | 40 |
Rangopalr | 72 |
Roy | 24 |
Ramna | 40 |
Ramna | 40 |
Query 3: The following SELECT query uses the BIT_LENGTH function with the First_City and New_City columns of the above Employee_Grade table:
This SQL statement shows the length of the first and new city of each employee in bits.
Output:
First_City | BIT_LENGTH_FirstCity | New_City | BIT_LENGTH_NewCity |
---|---|---|---|
Lucknow | 56 | Ghaziabad | 72 |
Aurangabad | 80 | Ghaziabad | 72 |
Noida | 40 | Lucknow | 56 |
Ghaziabad | 72 | Lucknow | 56 |
Noida | 40 | Ghaziabad | 72 |
Ghaziabad | 72 | Aurangabad | 80 |
Lucknow | 56 | Aurangabad | 80 |