MySQL LENGTH() Function Explained

The LENGTH() function in MySQL is used to retrieve the length of a string or binary data. It returns an integer representing the number of characters in the specified string or the number of bytes in the binary data.

Grammar: Rules governing the structure of a language.

LENGTH(str)

str is a string or binary data for which the length needs to be calculated.

Original: “他对这个主意很感兴趣。”
Paraphrased: “He is very interested in this idea.”

SELECT LENGTH('Hello World');

The output is 11 because the string ‘Hello World’ consists of 11 characters.

SELECT LENGTH('你好世界');

The output is 6 because the string ‘你好世界’ is made up of 6 characters.

SELECT LENGTH('abc123');

The output is 6 because the string ‘abc123’ is made up of 6 characters.

Please note:

  1. The LENGTH() function returns the number of characters for strings and the number of bytes for binary data.
  2. When using UTF-8 encoding, one character may occupy multiple bytes, so the returned length may not be the same as the apparent number of characters.
bannerAds