Mysqlchar_lengthlengthbit_length difference instance analysis

Source: Internet
Author: User
Mysql can use the char_lengthlengthbit_length functions to obtain the string length. However, the usage of the three functions is different. This article introduces the difference between mysqlchar_lengthlengthbit_length and some friends can refer to it. There are two situations:

1. GBK encoding

-- Both Chinese and Western characters are treated equally, so the total number of characters is 2.

Select char_length ('China ');

-- One Chinese character occupies 2 bytes, so the total byte length is 4

Select length ('China ');

-- One English character occupies 1 byte, so the total length of the byte is 5

select length('china');

-- One Chinese character occupies 2 bytes, and one byte occupies 8 digits, so the total bit length is 32

Select bit_length ('China ');

2. UTF-8 coding

-- Both Chinese and Western characters are treated equally, so the total number of characters is 2.

Select char_length ('China ');

-- One Chinese character occupies three bytes, so the total length of the byte is 6.

Select length ('China ');

-- One English character occupies 1 byte, so the total length of the byte is 5

select length('china'); 

-- One Chinese character occupies 3 bytes, and one byte occupies 8 digits, so the total bit length is 48

Select bit_length ('China ');

Related reading:

Mysql length (get string length) function instance analysis

Mysql char_length () get the string length

Mysql BIT_LENGTH () obtains the bit length of a string.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.