MariaDB CHARACTER_LENGTH() Function

In MariaDB, CHARACTER_LENGTH() is a built-in string function that returns the number of characters for a given string argument.

CHARACTER_LENGTH() is an alias for CHAR_LENGTH().

MariaDB CHARACTER_LENGTH() Syntax

Here is the syntax of the MariaDB CHARACTER_LENGTH() function:

CHARACTER_LENGTH(str)

Parameter Description

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 'CHARACTER_LENGTH'.

Return value

The MariaDB CHARACTER_LENGTH() function returns the number of characters for the given string argument. It will return NULL if the argument str is NULL.

CHARACTER_LENGTH() returns the number of characters, not the number of bytes. If you want to return the bytes of a string, use the LENGTH() function. If you want to return the bits in a string, use the BIT_LENGTH() function.

If you use the NULL parameter , the CHARACTER_LENGTH() function will return NULL.

MariaDB CHARACTER_LENGTH() Examples

The following statement returns the the number of characters of Hello using the MariaDB CHARACTER_LENGTH() function:

SELECT CHARACTER_LENGTH('Hello');

Output:

+---------------------------+
| CHARACTER_LENGTH('Hello') |
+---------------------------+
|                         5 |
+---------------------------+

For multi-byte characters, CHARACTER_LENGTH() is still considered it as a character, no matter how many bytes it contains.

The following statement uses the MariaDB CHARACTER_LENGTH() function to return the number of characters of 你好:

SELECT CHARACTER_LENGTH('你好');

Output:

+----------------------------+
| CHARACTER_LENGTH('你好')   |
+----------------------------+
|                          2 |
+----------------------------+

In this example, CHARACTER_LENGTH('你好') returns 2. Because there are only 2 characters in 你好.

Conclusion

The MariaDB CHARACTER_LENGTH() function returns the number of characters for the given string argument.