This function helps in getting the length of a string in bytes.
LENGTH() functions returns the length of a string in bytes. A multibyte character counts as a multiple bytes. This means that for a string containing five 2-byte characters, LENGTH() returns 10, whereas CHAR_LENGTH() returns 5. See in the examples.
[table caption=”” width=”100%” colwidth=”15%|15%|15%|55%” colalign=”left|lef|lef|left”]
Name, Required /Optional, Value Type, Description
string , Required, String , The input string to count the length.
[/table]
[table caption=”” width=”100%” colwidth=”20%|80%” colalign=”left|left”]
Returns,
Number, returns a length of characters in given string.
[/table]
[table caption=”” width=”100%” colwidth=”25%|75%” colalign=”left|left”]
Version, MySQL 4.0
[/table]
mmysql> SELECT LENGTH('tutorialmines'), CHAR_LENGTH('tutorialmines'); +-------------------------+------------------------------+ | LENGTH('tutorialmines') | CHAR_LENGTH('tutorialmines') | +-------------------------+------------------------------+ | 13 | 13 | +-------------------------+------------------------------+ 1 row in set (0.00 sec)
For same input both function returns different values because the section sign, § is a typographical glyph for referencing individually numbered sections of a document; it is frequently used when citing sections of a legal code. It is a multibyte characters.
mysql> SELECT length('§§§§§'), char_length('§§§§§'); +----------------------+---------------------------+ | length('§§§§§') | char_length('§§§§§') | +----------------------+---------------------------+ | 10 | 5 | +----------------------+---------------------------+ 1 row in set (0.02 sec)
For same input both function returns different values because the Euro sign € is a multibyte characters. The Euro sign uses 3 bytes.
mysql> SELECT length('€'), char_length('€'); +---------------+--------------------+ | length('€') | char_length('€') | +---------------+--------------------+ | 3 | 1 | +---------------+--------------------+ 1 row in set (0.00 sec)
It will return NULL, if the argument is NULL. Lets see the below example.
mysql> SELECT LENGTH(NULL); +--------------+ | LENGTH(NULL) | +--------------+ | NULL | +--------------+ 1 row in set (0.00 sec)
The LENGTH() function counts trailing blanks i.e. spaces at the end of the string. So if we add a space to the end of the first example, we get the following result:
mysql> SELECT LENGTH('tutorialmines '); +--------------------------+ | LENGTH('tutorialmines ') | +--------------------------+ | 14 | +--------------------------+ 1 row in set (0.00 sec)
The LENGTH() function counts trailing blanks i.e. spaces at the start of the string. So if we add a space to the start of the first example, we get the following result:
mysql> SELECT LENGTH(' tutorialmines'); +--------------------------+ | LENGTH(' tutorialmines') | +--------------------------+ | 14 | +--------------------------+ 1 row in set (0.00 sec)
See all MySQL String functions MySQL 8 String Functions.
App usage is growing steadily without showing any signs of slowing down. Hence, it is no surprise that mobile applications…
As the world has grown more digital, businesses have adapted themselves. An effectual adaptation includes online advertising. Offline advertising styles…
Step into a world where apps dance to the user's tune. Picture Instagram, a photo-sharing sensation that swept the globe.…
COVID-19 has led to a digitalization of lifestyle. As patients are taking their mental and physical health more seriously, healthcare…
Introduction WordPress, an immensely popular content management system (CMS), powers over 40% of the internet. What makes WordPress even more…
For moving companies trying to capture their market share amidst stiff competition, a tip or two about what they can…