How the LENGTH() function works in Mariadb?
The LENGTH()
function is a string function that returns the length of a given string in bytes.
The LENGTH()
function is a string function that returns the length of a given string in bytes. It can be used to measure the size of a string expression, such as a column value, a literal string, or a variable.
Syntax
The syntax of the LENGTH()
function is as follows:
LENGTH(string_expression)
The string_expression
is the string that you want to get the length of. It can be any valid string expression in Mariadb.
The LENGTH()
function returns an integer value that is the number of bytes in the input string. If the input string is NULL
, the function returns NULL
.
Examples
In this section, we will show some examples of how to use the LENGTH()
function in Mariadb.
Example 1: Getting the length of a literal string
The following example shows how to use the LENGTH()
function to get the length of a literal string in bytes.
SELECT LENGTH('Hello World!');
The output is:
12
As you can see, the LENGTH()
function returns the number of bytes in the input string, which is 12
.
Example 2: Getting the length of a column value
The following example shows how to use the LENGTH()
function to get the length of a column value in bytes. Suppose we have a table called customers
that stores the customer information, such as name, email, and country. The table has the following data:
id | name | country | |
---|---|---|---|
1 | Alice | [email protected] | USA |
2 | Bob | [email protected] | UK |
3 | Charlie | [email protected] | Canada |
4 | David | [email protected] | Australia |
We can use the LENGTH()
function to get the number of bytes in the name column, as shown below:
SELECT id, name, LENGTH(name) AS name_length, email, country FROM customers;
The output is:
id | name | name_length | country | |
---|---|---|---|---|
1 | Alice | 5 | [email protected] | USA |
2 | Bob | 3 | [email protected] | UK |
3 | Charlie | 7 | [email protected] | Canada |
4 | David | 5 | [email protected] | Australia |
As you can see, the LENGTH()
function returns the number of bytes in the name column for each row.
Example 3: Getting the length of a variable value
The following example shows how to use the LENGTH()
function to get the length of a variable value in bytes. Suppose we have a variable called @greeting
that stores a greeting message, such as “Hello World!”. We can use the LENGTH()
function to get the number of bytes in the variable value, as shown below:
SET @greeting = 'Hello World!';
SELECT LENGTH(@greeting);
The output is:
12
As you can see, the LENGTH()
function returns the number of bytes in the variable value, which is 12
.
Example 4: Getting the length of a string with special characters
The following example shows how to use the LENGTH()
function to get the length of a string with special characters in bytes. Suppose we have a string that contains some uppercase letters, some lowercase letters, some numbers, some symbols, and some accented characters, such as “A1B2C3!@#$%^&*()ÉÈÊË”. We can use the LENGTH()
function to get the number of bytes in the string, as shown below:
SELECT LENGTH('A1B2C3!@#$%^&*()ÉÈÊË');
The output is:
24
As you can see, the LENGTH()
function returns the number of bytes in the input string, which is 24
. Note that the accented characters take more than one byte to store, so the length of the string is not equal to the number of characters.
Related Functions
There are some other functions that are related to the LENGTH()
function in Mariadb. They are:
- The
CHAR_LENGTH()
function: This function returns the length of a given string in characters. It is similar to theLENGTH()
function, but it counts the number of characters instead of bytes. For example,CHAR_LENGTH('A1B2C3!@#$%^&*()ÉÈÊË')
returns16
. - The
BIT_LENGTH()
function: This function returns the length of a given string in bits. It is similar to theLENGTH()
function, but it multiplies the number of bytes by 8. For example,BIT_LENGTH('Hello World!')
returns96
. - The
LEFT()
function: This function returns the leftmost part of a given string, up to a specified number of characters. It can be used with theLENGTH()
function to get the whole string. For example,SELECT LEFT('Hello World!', LENGTH('Hello World!'))
returnsHello World!
.
Conclusion
In this article, we have learned how the LENGTH()
function works in Mariadb. We have seen the syntax of the function, and some examples of how to use it with different types of string expressions. We have also learned about some related functions that can be used with the LENGTH()
function. The LENGTH()
function is a useful function that can help us measure the size of strings in Mariadb.