Which function is a synonym of MySQL LENGTH() function?

MySQLMySQLi Database

As we know that, MySQL OCTET_LENGTH() function also measures the string length in ‘bytes’ hence, it is the synonym of MySQL LENGTH() function. The syntax of this function is OCTET_LENGTH(Str) where Str is a string whose length in characters has to be returned.

It is also not multi-byte safe like LENGTH() function. For example, if a string contains four 2-bytes characters then OCTET_LENGTH() function will return 8. It is demonstrated in the example below −

Example

mysql> Select OCTET_LENGTH('tutorialspoint');
+--------------------------------+
| OCTET_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 OCTET_LENGTH(@A);
+------------------+
| OCTET_LENGTH(@A) |
+------------------+
|               28 |
+------------------+
1 row in set (0.00 sec)
raja
Published on 20-Feb-2018 16:51:50
Advertisements