Home > Database > Mysql Tutorial > How does the MySQL LENGTH() function measure string length?

How does the MySQL LENGTH() function measure string length?

WBOY
Release: 2023-09-02 16:45:02
forward
1496 people have browsed it

MySQL LENGTH() 函数如何测量字符串长度?

The MySQL LENGTH() function measures string length in "bytes", which means it is not multibyte safe. The difference in results between multibyte-safe functions such as CHAR_LENGTH() or CHARACTER_LENGTH() and the LENGTH() function is particularly relevant to Unicode, where most characters are encoded in two bytes, or to UTF-8, The number of bytes varies. For example, if the string contains four 2-byte characters, the LENGTH() function returns 8, and the CHAR_LENGTH() or CHARACTER_LENGTH() function returns 4. As shown in the following example -

Example

mysql> Select LENGTH('tutorialspoint');
+--------------------------+
| LENGTH('tutorialspoint') |
+--------------------------+
|                       14 |
+--------------------------+
1 row in set (0.00 sec)
Copy after login

The above result set shows that the length of the string "tutorialspoint" is 14 because it has not been converted to Unicode characters. The following query converts it to Unicode characters -

mysql> SET @A = CONVERT('tutorialspoint' USING ucs2);
Query OK, 0 rows affected (0.02 sec)
Copy after login

After converting the string to Unicode, the result is 28 instead of 14 because in Unicode a single character occupies 2 bytes as shown below-

mysql> Select LENGTH(@A);
+------------+
| LENGTH(@A) |
+------------+
|         28 |
+------------+
1 row in set (0.00 sec)
Copy after login

But CHAR_LENGTH() gives result of 14 as it is multi-byte safe function as shown below-

mysql> Select CHAR_LENGTH(@A);
+-----------------+
| CHAR_LENGTH(@A) |
+-----------------+
|              14 |
+-----------------+
1 row in set (0.00 sec)
Copy after login

The above is the detailed content of How does the MySQL LENGTH() function measure string length?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template