What is the difference between mysqllength and char_length?

Source: Internet
Author: User
The two functions mysqllength and char_length must be familiar to everyone, but there should be very few people who know the difference between them. in this article, I would like to introduce to coders what is the difference between mysqllength and char_length and what is the difference? For more information, see. Length: the length of a calculated field. a Chinese character is counted as three characters. a number or letter is counted as one character.

Char_length: whether it is a Chinese character, a number, or a letter.

Example:

1. the data you want to query contains three characters in English. for this query, you need to use the mysql function of length, length is the length of a calculated field. a Chinese character is counted as three characters, and a number or letter is counted as one character.

Select * from user where length (username) = 3

In this way, the query results will show all the username fields with three characters, and no username with three Chinese characters will appear.

2. if you want to query a field that occupies three positions, you should use char_length for query, this function is a single character, regardless of Chinese characters, numbers, or letters,

Select * from user where char_length (username) = 3

Length: indicates the length of a calculated field. a Chinese character can contain three or four characters. it depends on the encoding of your database. a number or letter can be a character.

Note that for multi-byte characters, its CHAR_LENGTH () is calculated only once.

SELECT Name, CHAR_LENGTH (Name) AS CharLengthFROM DVDsWHERE CHAR_LENGTH (Name)> 5 order by Name;

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.