
- 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
How MySQL LENGTH() function measures the string length?
MySQL LENGTH() function measures the string length in ‘bytes’ which means that it is not multibyte safe. The difference of the result between multi-byte safe functions, like CHAR_LENGTH() or CHARACTER_LENGTH(), and LENGTH() function especially relevant for Unicode, in which most of the characters are encoded in two bytes or relevant for UTF-8 where the number of bytes varies. For example, if a string contains four 2-bytes characters then LENGTH() function will return 8, whereas CHAR_LENGTH() or CHARACTER_LENGTH() function will return 4. It is demonstrated in the example below −
Example
mysql> Select LENGTH('tutorialspoint'); +--------------------------+ | LENGTH('tutorialspoint') | +--------------------------+ | 14 | +--------------------------+ 1 row in set (0.00 sec)
The above result set shows that the length of string ‘tutorialspoint’ is 14 because it is yet not converted to Unicode character. The following query converts it into Unicode character −
mysql> SET @A = CONVERT('tutorialspoint' USING ucs2); Query OK, 0 rows affected (0.02 sec)
After converting the string in Unicode, it gives the result 28 instead of 14 because in Unicode a single character takes 2-bytes as shown below −
mysql> Select LENGTH(@A); +------------+ | LENGTH(@A) | +------------+ | 28 | +------------+ 1 row in set (0.00 sec)
But CHAR_LENGTH() gives the result as 14 because it is multi-byte safe function as shown below −
mysql> Select CHAR_LENGTH(@A); +-----------------+ | CHAR_LENGTH(@A) | +-----------------+ | 14 | +-----------------+ 1 row in set (0.00 sec)
- Related Articles
- How can I store the fixed length string as well as variable length string in the same MySQL table?
- How do I get the average string length in MySQL?
- Which function is a synonym of MySQL LENGTH() function?
- Which MySQL function can be used to find out the length of the string in bits?
- How can we retrieve the length of a specified string in MySQL?
- What is the difference between MySQL LENGTH() and CHAR_LENGTH() function?
- Java string length without using length() method.
- Query in MySQL for string fields with a specific length?
- How to calculate the length of the string using C#?
- How to get the longest VarChar length in MySQL?
- Java String length() method example.
- Python Program to Calculate the Length of a String Without Using a Library Function
- PHP – Get the string length using mb_strlen()
- How to concatenate the string value length -1 in JavaScript.
- How to get the length of a string in Python?
