MySQL Character Length Function
In this context, we will learn how we can use the MySQL CHARACTER_LENGTH() function with proper syntax and good examples.
Introduction of MySQL CHARACTER_LENGTH() function
CHARACTER_LENGTH() function in MySQL is used to find the length of a given string (in characters). It counts the number of characters and ignores whether the character(s) are single-byte or multi-byte.
Syntax of the MySQL CHARACTER_LENGTH() function
The syntax of the MySQL CHARACTER_LENGTH() function is given as follows:
CHARACTER_LENGTH(string)
Parameters or arguments used in MySQL CHARACTER_LENGTH() function:
Only one parameter is accepted by the CHARACTER_LENGTH() function in MySQL, which is given as follows:
String: This is the String whose length is to be calculated.
Returns:
It will return the length of a given string.
Application used for CHARACTER_LENGTH() function:
The CHARACTER_LENGTH() function can be used in the given below MySQL versions.:
- MySQL 5.7
- MySQL 5.6
- MySQL 5.5
- MySQL 5.1
- MySQL 5.0
- MySQL 4.1
- MySQL 4.0
- MySQL 3.23
Examples of MySQL CHARACTER_LENGTH() function:
Now we will look into some MySQL CHARACTER_LENGTH() function examples and will explore how we can use the CHARACTER_LENGTH function in MySQL.
Example-1 :
Here, we will derive the length for a String using the CHARACTER_LENGTH() function in MySQL.
SELECT CHARACTER_LENGTH("Javatpoint") AS LengthOfString
Output :
LengthOfString
10
1 row in set (0.00 sec)
Example-2 :
Here, we will derive the length for a String using the CHARACTER_LENGTH() function in MySQL.
SELECT CHARACTER_LENGTH("MYSQL Tutorial in javatpoint") AS LengthOfString
Output :
LengthOfString
28
1 row in set (0.00 sec)
Example-3 :
When we require to find the length of every String in a column of a Table, then we can take the help of the CHARACTER_LENGTH() function in MySQL.
CHARACTER_LENGTH() function finds the length for every String in a column of a Table.
Student_Roll | Student_Name | Total_Marks |
1 | Amit | 450 |
2 | Aniket | 470 |
3 | Sanjana | 432 |
4 | Nirmal | 475 |
Table – Student_Details
SELECT CHARACTER_LENGTH(Student_Name) AS LengthOfName FROM Student_Details;
Output :
LengthOfName
4
6
7
6
Application of MySQL CHARACTER_LENGTH() function:
This function is used to find the length of a given string (in characters).
Summary:
In the above context, we have learned how we can use the CHARACTER_LENGTH() function in MySQL used to find the length of a given string (in characters).