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 function. In MySQL, Character_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 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 is to count the number of single 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 Char_Length function counts the empty space as well.

MySQL CHAR_LENGTH Function Example 1

MySQL Char_Length Example 2

This also allows you to find the character length of the data inside a column. In this example, we are going to 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;

MySQL CHAR_LENGTH Function Example 2

MySQL Char_Length Function WHERE Condition

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

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, WHERE Clause does not understand the ALIAS Column name declared in the SELECT Statement. So, you have to use Character_Length method explicitly at where clause

MySQL CHAR_LENGTH Function Example 3