MySQL CHAR_LENGTH() Function
CHAR_LENGTH() function returns the number of characters in its argument.
You can also use the
CHARACTER_LENGTH() function, as it does the same thing.
Here's an example of using both functions:
In the above example, we select the
title field from the
film table. We then return the number of characters the value contains, first by using
CHAR_LENGTH(title), then by using
You can use conditional statements to return different results depending on the length of the string. For example, you could do this:
This time we use
CHAR_LENGTH() to check the length of the title. If the title is more than a certain number of characters, we add an ellipsis to the title.
It's important not to get confused between the
Also, don't forget that
CHARACTER_LENGTH() is a synonym of
CHAR_LENGTH(). And while we're talking about synonyms,
OCTET_LENGTH() is a synonym for
So, just to be clear…
- Returns the number of characters in its argument.
- Returns the length of a string in bytes (not the number of characters).
The examples on this page use the Sakila sample database.