MYSQL: length() vs char_length()

发布时间 2023-08-20 22:04:17作者: 小白冲冲

 

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.