MYSQL: length() vs char_length()

小白冲冲 / 2023-08-21 / 原文

 

select length('€'), char_length('€')
--> 1, 1

 

LENGTH() returns the length of the string measured in bytes.
CHAR_LENGTH() returns the length of the string measured in characters.

This is especially relevant for Unicode, in which most characters are encoded in two bytes. Or UTF-8, where the number of bytes varies. For example:

select length(_utf8 '€'), char_length(_utf8 '€')
--> 3, 1

 

CHAR_LENGTH() gives precise result than LENGTH() function.