MySQL Tutorial MySQL Advanced MySQL Database Account Management MySQL References

MySQL BIT_LENGTH() Function



The MySQL BIT_LENGTH() function returns the length of the specified string in bits.

Syntax

BIT_LENGTH(string)

Parameters

string Required. Specify the string to return the length for.

Return Value

Returns the length of the specified string in bits.

Example 1:

The example below shows the usage of BIT_LENGTH() function.

mysql> SELECT BIT_LENGTH('Learning SQL is FUN!');
Result: 160

mysql> SELECT BIT_LENGTH(NULL);
Result: NULL

mysql> SELECT BIT_LENGTH('');
Result: 0

mysql> SELECT BIT_LENGTH(' ');
Result: 8

mysql> SELECT BIT_LENGTH('SQL Tutorial');
Result: 96

Example 2:

Consider a database table called Employee with the following records:

EmpIDNameCityAgeSalary
1JohnLondon253000
2MarryNew York242750
3JoParis272800
4KimAmsterdam303100
5RameshNew Delhi283000
6HuangBeijing282800

The statement given below can be used to get the length of records of City column in bits.

SELECT *, BIT_LENGTH(City) AS BIT_LENGTH_Value FROM Employee;

The query will produce the following result:

EmpIDNameCityAgeBIT_LENGTH_Value
1JohnLondon2572
2MarryNew York2496
3JoParis2760
4KimAmsterdam30108
5RameshNew Delhi28108
6HuangBeijing2884

❮ MySQL Functions