The LENGTH function returns the byte length of a string (in bytes). This function calculates the number of bytes a string occupies in UTF-8 encoding, not the number of characters.
Note the difference from CHAR_LENGTH:
LENGTH() returns the number of bytesCHAR_LENGTH() and CHARACTER_LENGTH() return the number of charactersOCTET_LENGTH()LENGTH(<str>)
| Parameter | Description |
|---|---|
<str> | The string whose byte length needs to be calculated. Type: VARCHAR |
Returns INT type, representing the byte length of the string.
Special cases:
SELECT LENGTH('abc'), CHAR_LENGTH('abc');
+---------------+--------------------+ | LENGTH('abc') | CHAR_LENGTH('abc') | +---------------+--------------------+ | 3 | 3 | +---------------+--------------------+
SELECT LENGTH('中国'), CHAR_LENGTH('中国');
+------------------+---------------------+ | LENGTH('中国') | CHAR_LENGTH('中国') | +------------------+---------------------+ | 6 | 2 | +------------------+---------------------+
SELECT LENGTH(NULL);
+--------------+ | LENGTH(NULL) | +--------------+ | NULL | +--------------+