Skip to content

MySQL CHAR_LENGTH vs. LENGTH: Bytes, Unicode and Spaces

In MySQL, CHAR_LENGTH() counts code points in a nonbinary string, while LENGTH() counts its bytes. Compare both on the same expression. A larger byte count can be normal for Unicode text; neither result is a general count of visible characters. These examples use the MySQL 8.4 rules documented in the string-function reference.

Compare the exact Unicode input

The following are expected results for nonbinary utf8mb4 strings, derived from the documentation and checked with Unicode code-point and UTF-8 byte arithmetic. They are not results from a MySQL server test.

InputCode pointsUTF-8 bytes
A11
é (U+00E9)12
e + U+030123
😀 (U+1F600)14

The utf8mb4 character set supports supplementary characters such as this emoji. The two accented forms can look alike while containing different code points. Unicode grapheme boundaries describe user-perceived characters; a combining mark can belong to the same visible character as its base. Do not normalize input merely to make a count agree: that changes the text being measured.

Run a scoped comparison

With a UTF-8 encoded query file or client input, this read-only statement gives an expected code-point count of 1 and byte count of 2:

SELECT CHAR_LENGTH(_utf8mb4'é') AS code_points,
       LENGTH(_utf8mb4'é') AS bytes;

The character-set introducer labels the following literal's encoding. It does not convert incorrectly encoded bytes into UTF-8. Check how your client sends the query before using an accented literal as a diagnostic.

Check the column and retrieved spaces

In MySQL, VARCHAR(n) declares a character limit. Its effective capacity also depends on the row's byte limits and character set. A byte count alone therefore does not prove a value will fit, and expression bytes are not a measurement of total disk storage.

For values within the declared limit, VARCHAR retains trailing spaces. CHAR normally removes them on retrieval unless PAD_CHAR_TO_FULL_LENGTH is enabled. A length function measures the expression it receives, after that retrieval behavior. Excess trailing spaces beyond a VARCHAR limit can instead be truncated with a warning. The CHAR and VARCHAR documentation explains these exceptions. Inspect the original input and retrieved value before trimming anything.

Use the String Length guide to choose a counting unit, or the Unicode normalization guide to inspect composed and decomposed text. When working in another database, check its own rules; the PostgreSQL varchar guide covers a separate engine.