We know that MySQL's OCTET_LENGTH() function also measures the string length in "bytes", so it is a synonym for the MySQL LENGTH() function. The syntax of this function is OCTET_LENGTH(Str), where Str is the character string to be returned.
It also does not support multi-byte security like the LENGTH() function. For example, if a string contains four 2-byte characters, the OCTET_LENGTH() function will return 8. Demonstrated in the example below −
mysql> Select OCTET_LENGTH('tutorialspoint'); +--------------------------------+ | OCTET_LENGTH('tutorialspoint') | +--------------------------------+ | 14 | +--------------------------------+ 1 row in set (0.00 sec)
The above result set shows that the string "tutorialspoint" has a length of 14 because it has not been converted to Unicode characters. The following query converts it to Unicode characters −
mysql> SET @A = CONVERT('tutorialspoint' USING ucs2); Query OK, 0 rows affected (0.02 sec)
After converting the string to Unicode, the result is 28 instead of 14 because in Unicode, one character takes 2 bytes as shown below −
mysql> Select OCTET_LENGTH(@A); +------------------+ | OCTET_LENGTH(@A) | +------------------+ | 28 | +------------------+ 1 row in set (0.00 sec)
The above is the detailed content of Which function is synonymous with the MySQL LENGTH() function?. For more information, please follow other related articles on the PHP Chinese website!