CHARACTER_LENGTH() returns the number of characters in a string. It is especially useful when text may contain multibyte characters, because LENGTH() counts bytes instead.
SELECT CHARACTER_LENGTH('Welcome to plus2net') AS characters;
The result is 19.
For ordinary ASCII text the values are often the same. For multibyte text they can differ.
SELECT LENGTH(_utf8mb4 '€') AS bytes,
CHARACTER_LENGTH(_utf8mb4 '€') AS characters;
| bytes | characters |
|---|---|
| 3 | 1 |
CHAR_LENGTH() is a synonym for CHARACTER_LENGTH(). Use LENGTH() when you actually need byte length.A VARCHAR(100) column can store values up to its declared character limit, but individual rows may use fewer characters. Measure the stored value directly:
SELECT CHARACTER_LENGTH(title) AS character_count, title
FROM photo_img
WHERE img_id = 47;
To find the longest titles:
SELECT CHARACTER_LENGTH(title) AS character_count, title
FROM photo_img
ORDER BY character_count DESC;
You can also filter by character length:
SELECT title
FROM photo_img
WHERE CHARACTER_LENGTH(title) > 50
ORDER BY CHARACTER_LENGTH(title) DESC;
All String Functions · CHAR_LENGTH() · LENGTH() · SUBSTRING() · SUBSTRING_INDEX()
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.