In MySQL, there are many times where the length()
function and the char_length()
function will provide exactly the same results. However, there are also times where the results will be completely different. Here’s why.
First, here’s the definition for each of these functions:
char_length()
- Returns the length of a string, measured in characters.
length()
- Returns the length of a string, measured in bytes.
Notice “characters” vs “bytes” – one is measured in characters, the other is measured in bytes.
In many cases, the number of bytes will be the same as the number of characters in the string, but this isn’t always the case. The number of bytes used per character depends on how the data is stored. For example, if the string is stored as Unicode data, there will be 2 bytes per character.
Continue reading