MySQL Char_Length Function

MySQL Char_Length function is one of the String methods, which returns the length of the user-specified string measured in characters. It considers the multi-byte character as a single char. And this is the significant difference between Char_Length and Length functions.

The MySQL Char_length is the synonym for the standard Character Length method. So, you can use any of these method names.

MySQL Char_Length Syntax

The basic syntax of Character_Length in MySQL is as shown below:

SELECT CHAR_LENGTH (String_Expression)
FROM Source

To write this Character_Length Function using Command Prompt and Workbench, we are going to use the below-shown data

Employee Records Table 1

MySQL Char_Length Example

The char length function counts the number of characters inside a specified expression. The following query shows you multiple ways to use this character length.

SELECT CHAR_LENGTH ('MySQL') AS `Char Length`;

SELECT CHAR_LENGTH ('Learn MySQL Tutorials') AS `MySQLLength`;

SELECT CHAR_LENGTH ('Tutorial Gateway') AS `Website Length`;

Remember, the MySQL Char_Length function counts the empty space as well.

MySQL Char_Length Function Example 1

Example 2

This also allows you to find the character length of the data inside a column. In this example, we will find the char count of two columns: the Department Name and Email column.

SELECT  FirstName, 
        LastName, 
	    DepartmentName, 
        CHAR_LENGTH(DepartmentName) AS `Char Length of Dept`,
        Email,
        CHARACTER_LENGTH(Email) AS `Char Length of Email`
FROM employe;
Character length Example 2

MySQL Char_Length Function WHERE Condition

In this String method example, we show you how to use the Character length method at the MySQL where condition. Here, we used the char_length function in the where clause to check whether the character Length of a department column data is greater than 7.

SELECT  FirstName, 
        LastName, 
	    DepartmentName, 
        CHAR_LENGTH(DepartmentName) AS `Char Length of Dept`,
        Email,
        CHARACTER_LENGTH(Email) AS `Char Length of Email`
FROM employe
WHERE CHAR_LENGTH(DepartmentName) > 7
ORDER BY `Char Length of Dept`;

Remember, the WHERE Clause does not understand the ALIAS Column name declared in the SELECT Statement. So, you have to use the Character_Length method explicitly at the where clause.

MySQL Char_Length Function WHERE Condition Example 3