Home  >  Article  >  Database  >  Introduction to the usage of length function

Introduction to the usage of length function

DDD
DDDOriginal
2023-09-19 11:41:554861browse

The length function is used to return the number of characters or bytes of the specified string. It can be used to calculate the length of a string for operation and judgment when querying and processing string data. It should be noted that the length function counts the number of characters in the string, not the number of bytes. For multibyte character sets, a character may consist of multiple bytes. Therefore, the length function counts multibyte characters as one character when calculating the string length.

Introduction to the usage of length function

The length function is a string function in MySQL that returns the number of characters or bytes in a specified string. The length function can be used to calculate the length of a string for operation and judgment when querying and processing string data. The following is a detailed introduction and usage examples of the length function.

Syntax:

length(str)

Parameter description:

- str: The string whose length is to be calculated. Can be a string constant, variable or expression.

Return value:

The length function returns an integer value representing the number of characters or bytes in the string.

Note:

-The length function calculates the number of characters in the string, not the number of bytes. For multi-byte character sets (such as UTF-8), a character may consist of multiple bytes. Therefore, the length function counts multibyte characters as one character when calculating the string length.

- If the parameter is NULL, the length function returns NULL.

Example 1: Calculate the number of characters in a string

SELECT length('Hello World'); -- 返回11,因为字符串中有11个字符
SELECT length('你好,世界'); -- 返回7,因为字符串中有7个字符

Example 2: Calculate the number of bytes in a string

SELECT length('Hello World') * CHAR_LENGTH('Hello World'); -- 返回11,因为字符串中每个字符占用1个字节
SELECT length('你好,世界') * CHAR_LENGTH('你好,世界'); -- 返回21,因为字符串中每个字符占用3个字节

Example 3: Calculate the length of a string field in a table

CREATE TABLE students (
    id INT,
    name VARCHAR(50)
);
INSERT INTO students VALUES (1, 'Alice');
INSERT INTO students VALUES (2, 'Bob');
INSERT INTO students VALUES (3, 'Charlie');
SELECT name, length(name) FROM students;
-- 返回:
-- +---------+--------------+
-- | name    | length(name) |
-- +---------+--------------+
-- | Alice   | 5            |
-- | Bob     | 3            |
-- | Charlie | 7            |
-- +---------+--------------+

Example 4: Use the length function to make judgments in the query

SELECT name FROM students WHERE length(name) > 4;
-- 返回:
-- +---------+
-- | name    |
-- +---------+
-- | Alice   |
-- | Charlie |
-- +---------+

Summary:

The length function is a string function in MySQL. Used to count the number of characters or bytes in a specified string. The length function can be used to calculate the length of a string for operation and judgment when querying and processing string data. It should be noted that the length function counts the number of characters in the string, not the number of bytes. For multi-byte character sets, a character may consist of multiple bytes, so the length function counts multi-byte characters as one character when calculating the string length.

The above is the detailed content of Introduction to the usage of length function. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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