Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysql Length() for different languages returns different size

Tags:

mysql

select 

length('abcdefg') english,
length('가나다라마바사') korean

from dual

This returns below.

english korean
7       21

Is there any function returning the count of character not character bytes?

I mean what I need is

english korean
7       7
like image 562
Deckard Avatar asked Oct 04 '22 14:10

Deckard


2 Answers

use :

CHAR_LENGTH()

(the number of characters)

reference :

http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_char-length

like image 82
Nimrod007 Avatar answered Oct 08 '22 02:10

Nimrod007


See the manual:

Returns the length of the string str, measured in bytes. A multi-byte character counts as multiple bytes. This means that for a string containing five 2-byte characters, LENGTH() returns 10, whereas CHAR_LENGTH() returns 5.

CHAR_LENGTH() will do what you want.

like image 45
Pekka Avatar answered Oct 08 '22 04:10

Pekka