MariaDB LENGTHB() Function
In MariaDB, LENGTHB() is a built-in string function that returns the length of a given string in bytes.
LENGTHB() is a synonym for LENGTH() in default mode and has more explicit semantics, you should prefer it.
If you want to get the number of characters in a string, use the CHAR_LENGTH() or CHARACTER_LENGTH() function.
If you want to get the number of bits in a string, use the BIT_LENGTH() function.
MariaDB LENGTHB() Syntax
Here is the syntax of the MariaDB LENGTHB() function:
LENGTHB(str)
Parameters
str-
Required. The string whose length needs to be calculated. The parameter can be of other types, such as numbers or dates, etc., and the
LENGTHB()function will first convert it to a string and then calculate the length.
If you do not provide any parameters, MariaDB will report an error: ERROR 1582 (42000): Incorrect parameter count in the call to native function 'LENGTHB'.
Return value
The MariaDB LENGTHB(str) function returns the length in bytes of the specified string, which is the number of bytes.
If the argument is NULL, the LENGTHB() function will return NULL.
MariaDB LENGTHB() Examples
This statement shows various basic uses of the MariaDB LENGTHB() function:
SELECT
LENGTHB('Hello'),
LENGTHB(''),
LENGTHB(20),
LENGTHB(-20),
LENGTHB(+20),
LENGTHB(NOW()),
LENGTHB(CURDATE()),
LENGTHB('你好'),
LENGTHB(NULL)\G
Output:
*************************** 1\. row ***************************
LENGTHB('Hello'): 5
LENGTHB(''): 0
LENGTHB(20): 2
LENGTHB(-20): 3
LENGTHB(+20): 2
LENGTHB(NOW()): 19
LENGTHB(CURDATE()): 10
LENGTHB('你好'): 6
LENGTHB(NULL): NULLNotice:
- The result
LENGTHB(-20)is3. This is because the corresponding string of-20is'-20'andLENGTHB('-20')returns3. - The result
LENGTHB(+20)is2. This is because+20is equivalent to20, the corresponding string is'20', andLENGTHB('20')returns2. LENGTHB(NOW())is equivalent toLENGTHB('2021-04-02 21:18:57').- The result of
LENGTHB('你好')is6. This is because the encoding I use here isUTF-8and a Chinese character occupies 3 bytes.
Conclusion
The MariaDB LENGTHB() function returns the length of the given string in bytes.