MariaDB CHAR_LENGTH() Function
In MariaDB, CHAR_LENGTH() is a built-in string function that returns the number of characters for a given string argument.
CHAR_LENGTH() is an alias for CHARACTER_LENGTH().
MariaDB CHAR_LENGTH() Syntax
Here is the syntax of the MariaDB CHAR_LENGTH() function:
CHAR_LENGTH(str)
Parameters
str-
Required. The string whose length needs to be calculated.
If a parameter is missing, MariaDB will report an error: ERROR 1582 (42000): Incorrect parameter count in the call to native function 'CHAR_LENGTH'.
Return value
The MariaDB CHAR_LENGTH() function returns the number of characters for the given string argument. It will return NULL if the argument str is NULL.
CHAR_LENGTH() returns the number of characters, not the number of bytes. If you want to return the byte of a string, use the LENGTH() function. If you want to return the number of bits in a string, use the BIT_LENGTH() function.
If you use the NULL parameter, the CHAR_LENGTH() function will return NULL.
MariaDB CHAR_LENGTH() Examples
The following statement returns the the number of characters of Hello using the MariaDB CHAR_LENGTH() function:
SELECT CHAR_LENGTH('Hello');
Output:
+----------------------+
| CHAR_LENGTH('Hello') |
+----------------------+
| 5 |
+----------------------+For multi-byte characters, CHAR_LENGTH() is still considered it as a character, no matter how many bytes it contains.
The following statement uses the MariaDB CHAR_LENGTH() function to return the number of characters of 你好:
SELECT CHAR_LENGTH('你好');
Output:
+-----------------------+
| CHAR_LENGTH('你好') |
+-----------------------+
| 2 |
+-----------------------+In this example, CHAR_LENGTH('你好') returns 2. Because there are only 2 characters in 你好.
Conclusion
The MariaDB CHAR_LENGTH() function returns the number of characters for the given string argument.