w3resource

MySQL CHARACTER_LENGTH() function

CHARACTER_LENGTH()

MySQL CHARACTER_LENGTH() returns the length of a given string. The length is measured in characters. The CHARACTER_LENGTH() is the synonym of CHAR_LENGTH().

Syntax:

 
CHARACTER_LENGTH (str)

Argument:

Name Description
str A string whose length is to be retrieved.

Syntax Diagram:

MySQL CHARACTER_LENGTH() Function - Syntax Diagram

MySQL Version: 8.0

MySQL: CHARACTER_LENGTH() Function - w3resource

Example : MySQL CHARACTER_LENGTH() function

The following MySQL statement counts the length of the string specified as an argument.

Code:

SELECT CHARACTER_LENGTH('test string');

Output:

mysql> SELECT CHARACTER_LENGTH('test string');
+---------------------------------+
| CHARACTER_LENGTH('test string') |
+---------------------------------+
|                              11 | 
+---------------------------------+
1 row in set (0.00 sec)

Example of MySQL character_length() function where clause

The following MySQL statement counts only those of the publisher's names (from column pub_name of the publisher table) which are more than 20 characters long.

Code:

SELECT pub_name,CHARACTER_LENGTH(pub_name)
AS 'character length'
FROM publisher
WHERE CHARACTER_LENGTH(pub_name)>20;

Sample table: publisher


Output:

mysql> SELECT pub_name,CHARACTER_LENGTH(pub_name)
    -> AS 'character length'
    -> FROM publisher
    -> WHERE CHARACTER_LENGTH(pub_name)>20;
+------------------------------+------------------+
| pub_name                     | character length |
+------------------------------+------------------+
| New Harrold Publication      |               23 | 
| Summer Night Publication     |               24 | 
| Pieterson Grp. of Publishers |               28 | 
+------------------------------+------------------+
3 rows in set (0.00 sec)

Video Presentation:

All String Functions (Slides presentation)

Previous: CHAR
Next: CONCAT_WS



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/mysql/string-functions/mysql-character_length-function.php