MySQL LENGTH() Function
In this context, we will learn how we can use the MySQL LENGTH() function with proper syntax and good examples.
Introduction of MySQL LENGTH() function
This function in MySQL is used to find the string length of type bytes.
Features of the MYSQL LENGTH function:
The string length of type bytes is found by the use of this function.
This function comes under String Functions.
This function accepts only one parameter, namely string.
This function returns the length in bytes only.
Syntax of the MySQL LENGTH() function
The syntax of the MySQL LENGTH() function is given as follows:
LENGTH(str)
Parameters or arguments used in MySQL LENGTH() function:
There is only one parameter accepted by the LENGTH() function in MySQL, which is given as follows:
- This method accepts only one parameter.
- Str – This is the mentioned string whose length is to be counted by this function.
Returns:
It will return the string length, which is of type bytes.
Application used for LENGTH() function:
The LENGTH() function can be used in the given below MySQL versions.:
- MySQL 5.7
- MySQL 5.6
- MySQL 5.5
- MySQL 5.1
- MySQL 5.0
- MySQL 4.1
- MySQL 4.0
- MySQL 3.23
Examples of MySQL LENGTH() function:
Now we will look into some MySQL LENGTH() function examples and will explore how we can use the LENGTH function in MySQL.
Example-1 :
Here, we will use the LENGTH() function to get the output.
SELECT LENGTH("JAVATPOINT");
Output:
10
1 row in set (0.00 sec)
Example-2 :
Here, we will use the LENGTH() function to find the length of all the float values.
CREATE TABLE float061
(
user_id int NOT NULL AUTO_INCREMENT,
float_val float,
PRIMARY KEY(user_id)
);
INSERT float061(float_val)
VALUES (1.9);
INSERT float061(float_val)
VALUES (1.1);
INSERT float061(float_val)
VALUES (3.9);
INSERT float061(float_val)
VALUES (10.2);
INSERT float061(float_val)
VALUES (7.9);
SELECT LENGTH(float_val) FROM float061;
Output:
LENGTH(float_val) |
3 |
3 |
3 |
4 |
3 |
Example-3 :
Here, we will use the LENGTH() function to get the length of the item given.
CREATE TABLE package156
(
user_id int NOT NULL AUTO_INCREMENT,
item VARCHAR(10),
mrp int,
PRIMARY KEY(user_id)
);
INSERT package156(item, mrp)
VALUES ('books', 350);
SELECT LENGTH(item) FROM package156;
Output:
5
Example-4:
In this example, we will use the LENGTH() function to get the length of all the (MRP+sales price) values.
CREATE TABLE package72
(
user_id int NOT NULL AUTO_INCREMENT,
item VARCHAR(10),
mrp int,
sp int,
PRIMARY KEY(user_id)
);
INSERT package72(item, mrp, sp)
VALUES ('book1', 250, 245);
INSERT package72(item, mrp, sp)
VALUES ('book2', 350, 345);
INSERT package72(item, mrp, sp)
VALUES ('book3', 400, 350);
SELECT LENGTH(mrp+sp) FROM package72;
Output:
LENGTH(mrp+sp) |
3 |
3 |
3 |
Application of MySQL LENGTH() function
This function is used to find the string length of type bytes.
Summary:
In the above context, we have learned how to use the LENGTH() function in MySQL to find the string length of type bytes.