Get the App
SLTechnology News&Howtos  ›  Database  › 

How to query character length by mysql

Shulou Source: shulou.com Published: 2022-05-31 12:19:42 10月03日 Update

This article will explain in detail how to query character length in mysql. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

Methods: 1, the use of length () function query, the unit is bytes, a Chinese character is three bytes, the array and letters are a byte, the syntax is "length (character)"; 2, the use of "char_length ()" function query, units for characters, Chinese characters, numbers and letters are a character unit.

The operating environment of this tutorial: windows10 system, mysql8.0.22 version, Dell G3 computer.

Take a look at the difference between length () and char_length () in Mysql.

1), length (): the length () function in mysql is a built-in function to get the length of a string.

2), char_length (): another function to check the string length in the mysql built-in function is char_length ().

3) the difference between the two functions is:

A), length (): units are bytes, under utf8 coding, a Chinese character has three bytes, a number or letter a byte. Under gbk coding, a Chinese character has two bytes, a number or letter a byte.

B), char_length (): units are characters, no matter Chinese characters or numbers or letters are considered as a character.

To expand, the change in varchar type since the MySQL5.0.3 version.

1), before MySQL 5.0.3: 0Murray 255 bytes, for example, 20 in varchar (20) indicates the number of bytes, if you store utf-8 code, you can only put 6 Chinese characters. Varchar (n), where n represents the number of bytes.

After MySQL 5.0.3: 0Mui 65535 bytes, varchar (20) represents the number of characters, no matter what kind of coding, both Chinese characters can also put 20. But it takes up to 65532 bytes (two bytes are stored in length, less than 255bytes are stored in one byte). The n here in varchar (n) represents the number of characters, such as varchar. Other specific differences can be queried online by yourself.

4. Length () char_length (), which can be used to check whether it contains Chinese characters.

In utf-8 coding, it is determined that a field is all English, and length (field) = char_length (field) is fine.

This is the end of this article on "how to query character length in mysql". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Tags: Byte character function Chinese character length query unit letter code number two field article three string more version or utf-8 Chinese Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Docker MySQL Xiaomi Shulou Tech Info