MySQL LENGTH Function: Get String Length

In MySQL, the LENGTH() function is used to get the length (number of bytes) of a string. It is important to remember that this function operates on bytes, not characters. For a multibyte character set such as UTF-8, this means that a single character could be more than 1 byte.

Syntax

Here's the basic syntax of the LENGTH() function:

LENGTH(string)
  • string: This is the original string for which you want to get the length.

Examples

Let's look at a few examples of how to use the LENGTH() function.

Example 1:

SELECT LENGTH('Hello');

Output:

5

In this example, we're finding the length of the string 'Hello', which is 5 bytes.

Example 2:

Suppose we have a table named employees with the following data:

IDName
1John Doe
2Jane
3Bob Smith

And we want to find the length of each name. We can use the LENGTH() function:

SELECT ID, Name, LENGTH(Name) as Length
FROM employees;

Output:

IDNameLength
1John Doe8
2Jane4
3Bob Smith9

In this example, the LENGTH() function is used to find the length of each name in the employees table.

Note: If you want to get the length in terms of characters, you should use the CHAR_LENGTH() function or LENGTH() function in combination with CONVERT() function. This is particularly important when dealing with strings that contain multibyte characters, as the number of bytes may not equal the number of characters. For example, in a UTF-8 database, '��' is a two-byte character, so LENGTH('��') would return 2, whereas CHAR_LENGTH('��') would return 1.

  1. MySQL LENGTH function example:

    • Description: The LENGTH function in MySQL is used to get the length of a string in bytes.
    • Example:
      SELECT LENGTH('Hello, World!') AS StringLength;
      -- Output: 13
      
  2. How to use LENGTH function in MySQL:

    • Description: Use the LENGTH function to determine the length of a string in MySQL.
    • Example:
      SELECT LENGTH('MySQL is great!') AS StringLength;
      -- Output: 16
      
  3. Get string length in MySQL:

    • Description: Retrieve the length of a string using the LENGTH function.
    • Example:
      SELECT LENGTH('This is a string') AS StringLength;
      -- Output: 16
      
  4. Character count in MySQL using LENGTH:

    • Description: Although named LENGTH, it counts bytes, not characters. Use CHAR_LENGTH for character count.
    • Example:
      SELECT LENGTH('Caf��') AS ByteLength, CHAR_LENGTH('Caf��') AS CharCount;
      -- Output: ByteLength: 5, CharCount: 4
      
  5. Differences between LENGTH and CHAR_LENGTH in MySQL:

    • Description: LENGTH counts bytes, while CHAR_LENGTH counts characters in MySQL.
    • Example:
      SELECT LENGTH('Hello, World!') AS ByteLength, CHAR_LENGTH('Hello, World!') AS CharCount;
      -- Output: ByteLength: 13, CharCount: 12
      
  6. Counting bytes with LENGTH in MySQL:

    • Description: Use LENGTH to count bytes, which is important for multibyte character sets.
    • Example:
      SELECT LENGTH('���') AS ByteLength;
      -- Output: 6 (assuming utf8mb4 encoding)
      
  7. MySQL string length limit:

    • Description: Be aware of the maximum length of strings in MySQL (varies based on data types).
    • Example:
      SELECT LENGTH('12345678901234567890'); -- Output: 20
      
  8. Finding the length of a text field in MySQL:

    • Description: Use LENGTH to find the length of a text field in MySQL.
    • Example:
      SELECT LENGTH(text_column) AS TextLength FROM your_table;
      
  9. Examples of using LENGTH function in MySQL queries:

    • Description: Demonstrate various use cases of the LENGTH function in MySQL queries.
    • Examples:
      SELECT LENGTH('Example 1: Length of string') AS Result1,
             LENGTH('Example 2: Another string') AS Result2;