Home >Database >Mysql Tutorial >How to use the LENGTH() function in MySQL

How to use the LENGTH() function in MySQL

WBOY
WBOYforward
2023-05-26 21:40:413589browse

LENGTH()

LENGTH(str) and OCTET_LENGTH(str) functions are used to return the byte length of a string, for example:

SELECT LENGTH('MySQL字符串函数') AS len1, OCTET_LENGTH('MySQL字符串函数') AS len2;
len1|len2|
----+----+
  20|  20|

In utf8mb4 encoding, one Chinese character occupies 3 bytes.

In addition, the CHAR_LENGTH(str) and CHARACTER_LENGTH(str) functions are used to return the character length of the string, which is the number of characters. For example:

SELECT CHAR_LENGTH('MySQL字符串函数') AS len1, CHARACTER_LENGTH('MySQL字符串函数') AS len2;
len1|len2|
----+----+
  10|  10|

BIT_LENGTH(str)The function is used to return the bit length (number of bits) of a string, for example:

SELECT BIT_LENGTH('MySQL字符串函数') AS len;
len|
---+
160|

A byte contains 8 bits.

The above is the detailed content of How to use the LENGTH() function in MySQL. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete