MySQL BIT_LENGTH() Function

In MySQL, the BIT_LENGTH() function return the length of a given string in bits.

If you want to return the length of a string in bytes, use the LENGTH() function.

If you want to return the number of characters in a string, use the CHAR_LENGTH() or CHARACTER_LENGTH() function.

BIT_LENGTH() Syntax

Here is the syntax of MySQL BIT_LENGTH() function:

BIT_LENGTH(string)

Parameter Description

string
Required. The string to be calculated.

Return value

The BIT_LENGTH() function returns length of the given string in bits. The value returned by the BIT_LENGTH() function is 8 times the value returned by the LENGTH() function, because 1 byte is equal to 8 bits.

  • If the parameter is not a string, the BIT_LENGTH() function will try to convert the parameter to a string.
  • It will return NULL if the string parameter is NULL.

BIT_LENGTH() Examples

SELECT
    BIT_LENGTH('a'),
    BIT_LENGTH('string'),
    BIT_LENGTH(1),
    BIT_LENGTH(01),
    BIT_LENGTH('01'),
    BIT_LENGTH('你好'),
    BIT_LENGTH(NULL)\G
     BIT_LENGTH('a'): 8
BIT_LENGTH('string'): 48
       BIT_LENGTH(1): 8
      BIT_LENGTH(01): 8
    BIT_LENGTH('01'): 16
BIT_LENGTH('你好'): 48
    BIT_LENGTH(NULL): NULL

Note that the result of BIT_LENGTH(01) and BIT_LENGTH('01') are not the same, this is because 01 is a number and it is as same as 1.