Search code examples
mysqlvarchar

How to determine amount of bytes in VARCHAR field value?


I have a mysql field with datatype VARCHAR(5) with utf encoding. If I understand correctly field values can hold up to 5 characters and since I'm using utf-8 encoding, a max of 20+1 bytes.

Is there any way I can determine how much space a field value is taking up?

For example I expect the following to take up more than 5 bytes

رروحي

But for this to take up exactly 5 bytes

hello

And for this to take up more than 5 bytes

分別以後才

Solution

  • IIRC, mysql has length and char_length functions, so pick the one you need. Obviously, one is supposed to return length in bytes, the other — in characters.

    If you want to find it out before it gets to the server side, you need to mention the programming language you use.