
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
length() vs char_length() in MySQL?
The char_length() can be used to display the length of a string. Let us see an example to get the length of the string included as a parameter.
mysql> select char_length('John');
The following is the output.
+---------------------+ | char_length('John') | +---------------------+ | 4 | +---------------------+ 1 row in set (0.00 sec)
The length() function can be used to display the length of string measured in bytes. In many cases characters and bytes gives the same length.
Here is an example of length()
mysql> select length('Tim');
The following is the output.
+---------------------+ | length('Tim') | +---------------------+ | 3 | +---------------------+ 1 row in set (0.00 sec)
The length() and char_length() gives the same result in string. The length() function gives different length when it applies Unicode. Suppose a column name has unicode.In Unicode, every single character takes 2 bytes.
Let us see an example −
mysql> create table LengthAndCharLengthDemo -> ( -> FirstName varchar(200), -> SecondName varchar(255) unicode -> ); Query OK, 0 rows affected (0.49 sec)
Inserting records into table.
mysql> insert into LengthAndCharLengthDemo values('John','Ritter'); Query OK, 1 row affected (0.22 sec)
Displaying all records from the table with the help of select.
mysql> select *from LengthAndCharLengthDemo;
Here is the output.
+-----------+------------+ | FirstName | SecondName | +-----------+------------+ | John | Ritter | +-----------+------------+ 1 row in set (0.00 sec)
Let us get the length.
mysql> select char_length(FirstName),length(Secondname) from LengthAndCharLengthDemo -> where FirstName='John' and Secondname='John';
Here is the output.
+------------------------+--------------------+ | char_length(FirstName) | length(SecondName) | +------------------------+--------------------+ | 4 | 8 | +------------------------+--------------------+ 1 row in set (0.70 sec)
In the above output, you can see the length() function returns 8 bytes because of unicode while char_length() returns only 4.
- Related Articles
- What is the difference between MySQL LENGTH() and CHAR_LENGTH() function?
- How to use CHAR_LENGTH() in MySQL CREATE TABLE query?
- Implement SELECT LIKE and CHAR_LENGTH() in a single MySQL query
- How can we use CHAR_LENGTH() function with MySQL WHERE clause?
- What MySQL CHAR_LENGTH() function returns if no parameter is provided to it?
- What will MySQL CHAR_LENGTH() function return if I provide NULL to it?
- CURDATE () vs NOW() in MySQL
- WHERE vs HAVING in MySQL?
- MySQL ON vs USING?
- What is the purpose of using MySQL CHAR_LENGTH() function? Which function is the synonym of it?
- SELECT DISTINCT vs GROUP BY in MySQL?
- int(5) vs. int(10) in MySQL?
- System variables vs Local Variables in MySQL?
- User-defined variables vs Local Variables in MySQL?
- System variables vs User-defined variables in MySQL?
