Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Handout on the usage of CHARACTER_LENGTH function

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces you the handout on the usage of CHARACTER_LENGTH function, hoping to supplement and update some knowledge for you. If you have any other questions you need to know, you can continue to follow my updated article in industry information.

In MySQL, the CHARACTER_LENGTH () function returns the length of the string, in characters.

CHARACTER_LENGTH () is synonymous with the CHAR_LENGTH () function.

The syntax goes like this:

CHARACTER_LENGTH (str)

Where str is a string that returns the length.

Example 1-basic usage

Here is an example of basic usage:

SELECT CHARACTER_LENGTH ('Cat')

The result is this:

+-| CHARACTER_LENGTH ('Cat') | +-+ | 3 | +-+

Example 2-A space at the end

Note that CHARACTER_LENGTH () includes trailing spaces (such as those at the end of a string) in its calculation.

So if we add a space at the end of the previous example:

SELECT CHARACTER_LENGTH ('Cat')

Results:

+-- + | CHARACTER_LENGTH ('Cat') | +-+ | 4 | +-+

But we can use the TRIM () function or the RTRIM () function to remove the following spaces:

SELECT CHARACTER_LENGTH (TRIM ('Cat')) AS 'TRIM', CHARACTER_LENGTH (RTRIM (' Cat')) AS 'RTRIM'

Results:

+-+ | TRIM | RTRIM | +-+-+ | 3 | 3 | +-+-+

Example 3-preceded by a space

The same concept applies to the preceding spaces. You can use TRIM or LTRIM:

SELECT CHARACTER_LENGTH (TRIM ('Cat')) AS' TRIM', CHARACTER_LENGTH (LTRIM ('Cat')) AS' LTRIM'

Results:

+-+ | TRIM | LTRIM | +-+-+ | 3 | 3 | +-+-+

Example 4-data type

No matter what data type the string stores, it returns the same result. This contrasts with the LENGTH () function, which returns double the number of characters if the data is stored in a Unicode string.

In the following example, the ArtistName column uses varchar:

SELECT CHARACTER_LENGTH (ArtistName) ResultFROM ArtistsWHERE ArtistName = 'Lit'

Results:

+-+ | Result | +-+ | 3 | +-+

If we modify the ArtistName column to use Unicode:

ALTER TABLE Artists MODIFY COLUMN ArtistName VARCHAR (255) unicode

And run the same query again:

SELECT CHARACTER_LENGTH (ArtistName) FROM ArtistsWHERE ArtistName = 'Lit'

We still get the same result:

+-+ | Result | +-+ | 3 | +-+

However, if you use the LENGTH () function, the result will be 6. This is because the Unicode string stores 2 bytes per character, and the LENGTH () function returns the length measured in bytes.

Correlation

These are the details of the usage of the CHARACTER_LENGTH () function in MySQL. Please pay more attention to other related articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report