MYSQL: length() vs char_length()

 

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.

posted @ 2023-08-20 21:56  小白冲冲  阅读(3)  评论(0编辑  收藏  举报