LENGTH()
Returns the number of characters in a string.
Description
LENGTH counts the number of characters in a string. It's useful for
validating data (like checking that a code has a fixed number of
characters), sorting by how long a value is, or filtering out unusually
short or long text.
Syntax
LENGTH(string)Parameters
| Name | Description | Optional |
|---|---|---|
| string | The text value or column to measure. | No |
Return Type
LENGTH returns a BIGINT, the character count.
Examples
Movies (Movies/Actors/Ratings)
Loading database engine...
Movies (Movies/Actors/Ratings)
Loading database engine...
LENGTH counts characters, not bytes
For most text this matches what you'd expect, but with multi-byte
characters (accents, emoji, non-Latin scripts), LENGTH still counts
characters, not raw storage bytes, which is usually what you want.
Common Mistakes
- Confusing
LENGTHwith counting words.LENGTH('The Matrix')returns10(including the space), not the number of words. - Forgetting that leading or trailing spaces count.
'Avatar 'with a trailing space has a different length than'Avatar', which can cause confusing off-by-one results if the data has inconsistent spacing. - Applying
LENGTHto a NULL value and expecting0.LENGTH(NULL)returns NULL, not0, since there's no string to measure.
Related Functions
See also: SUBSTRING, CONCAT, UPPER.