MariaDB OCTET_LENGTH() Function
In MariaDB, OCTET_LENGTH() is a built-in string function that returns the length in bytes of the given string argument.
OCTET_LENGTH() is the same as the LENGTHB() function. It is also the same as the LENGTH() function in default mode.
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 OCTET_LENGTH() Syntax
Here is the syntax of the MariaDB OCTET_LENGTH() function:
OCTET_LENGTH(string)
Parameters
string-
Required. The string whose length needs to be calculated. The parameter can be of other types, such as numbers or dates, etc., and the
OCTET_LENGTH()function will convert it to a string first and then calculate the length.
If you do not provide a parameter, MariaDB will report an error: ERROR 1582 (42000): Incorrect parameter count in the call to native function 'OCTET_LENGTH'.
Return value
The MariaDB OCTET_LENGTH(string) function returns the length in bytes of the specified string, which is the number of bytes.
If the argument is NULL, the OCTET_LENGTH() function will return NULL.
MariaDB LENGTHB() Examples
This statement shows various basic uses of the MariaDB LENGTHB() function:
SELECT
OCTET_LENGTH('Hello'),
OCTET_LENGTH(''),
OCTET_LENGTH(20),
OCTET_LENGTH(-20),
OCTET_LENGTH(+20),
OCTET_LENGTH(NOW()),
OCTET_LENGTH(CURDATE()),
OCTET_LENGTH('你好'),
OCTET_LENGTH(NULL)\G
Output:
*************************** 1\. row ***************************
OCTET_LENGTH('Hello'): 5
OCTET_LENGTH(''): 0
OCTET_LENGTH(20): 2
OCTET_LENGTH(-20): 3
OCTET_LENGTH(+20): 2
OCTET_LENGTH(NOW()): 19
OCTET_LENGTH(CURDATE()): 10
OCTET_LENGTH('你好'): 6
OCTET_LENGTH(NULL): NULLNotice:
- The result of
OCTET_LENGTH(-20)is3. This is because-20is converted to'-20'andOCTET_LENGTH('-20')returns3. - The result of
OCTET_LENGTH(+20)is2. This is because+20is converted to20, and will be converted to'20', soOCTET_LENGTH('20')returns2. OCTET_LENGTH(NOW())is equivalent toOCTET_LENGTH('2021-04-02 21:18:57').- The result of
OCTET_LENGTH('你好')is6. This is because the encoding isUTF-8and a Chinese character occupies 3 bytes.
Conclusion
The MariaDB LENGTHB() function returns the length of the given string in bytes.