×

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.


Related Topics

MySQL Date and Time function

The date function displays day, year, month, time, and current date. It shows the date and time as per the requirement of the applications. The data either store on the...

8 minutes read.

MySQL IF statement

The IF statement shows the true condition of the control flow function. The first condition is necessary to fulfill the requirement. The other condition is optional in the "IF" statement....

3 minutes read.

MySQL MIN function

The MIN() function determines the minimum or lowest value of the data set. This function works on numerical data type values. If the table displays zero value, then the row...

4 minutes read.

MySQL INSERT() Function

In this context, we will learn how we can use the MySQL INSERT() function with proper syntax and good examples. Introduction of MySQL INSERT() function Basically, in MySQL, the INSERT function is...

2 minutes read.

MySQL SOUNDEX() function

In this context, we will learn how we can use the MySQL SOUNDEX() function with proper syntax and good examples. Introduction of MySQL SOUNDEX() function SOUNDEX() function in MySQL is used to...

3 minutes read.

MySQL EQUI JOIN

An equijoin is an operation that combines multiple tables based on equality or matching column values in the associated tables. This operation links more than two tables based on a...

5 minutes read.

MySQL PI() function

In this context, we will learn how we can use the MySQL PI() function with proper syntax and good examples. Introduction of MySQL PI() function PI() function in MySQL is used to...

2 minutes read.

MySQL PERIOD_ADD() function

In this context, we will learn how we can use the MySQL PERIOD_ADD() function with proper syntax and good examples. Introduction of MySQL PERIOD_ADD() function In MySQL, PERIOD_ADD() function will help to...

2 minutes read.

MySQL LEAST function

This statement displays the smallest value of the table. The LEAST function returns a null value when the table contains a null value. If the table contains all numerical values,...

2 minutes read.

MySQL COUNT function

The count function returns the number of rows in the table. This function shows either the entire rows count or the required row count of the table. It determines the...

5 minutes read.

MySQL Subquery

The subquery is a MySQL query used to manage data operations. Mainly subquery helps to retrieve data with the necessary condition. It creates a nested query with two different queries....

5 minutes read.

MySQL Descending Index

Descending Index It is a type of index that stores key values in descending order. This query improves the performance of an index query. MySQL system displays the index as per...

3 minutes read.

MySQL Create index

An index creation helps to make a row of the table unique. The index operates and handles table data quickly. MySQL index requires NOT NULL column constraint. The index column...

2 minutes read.

MySQL Database Introduction

MySQL - Database Introduction: The database plays an essential role in MySQL for storing and modifying the data. The database creates and keeps multiple tables. The database organizes and manipulates...

8 minutes read.

MySQL LOAD_FILE() function

In this context, we will learn how we can use the MySQL LOAD_FILE() function with proper syntax and good examples. Introduction of MySQL LOAD_FILE() function The LOAD_FILE() function in MySQL reads a...

3 minutes read.

MySQL RAND() function

In this context, we will learn how we can use the MySQL RAND() function with proper syntax and good examples. Introduction of MySQL RAND() function Basically, in MySQL the RAND() function is...

3 minutes read.

MySQL LOG10() function

In this context, we will learn how we can use the MySQL LOG10() function with proper syntax and good examples. Introduction of MySQL LOG10() function To evaluate the natural logarithmic value of...

2 minutes read.

MySQL NATURAL JOIN

The natural join combines multiple tables using a column with the same name and data type. This operation combines a row of the two tables using common columns. This join...

4 minutes read.

MySQL DELETE JOIN

Sometimes join becomes complicated and unwanted. In such cases, we can delete the unwanted joins in the tables. You can delete inner join, left join, right join as per requirement....

3 minutes read.

MySQL LOCATE() function

In this context, we will learn how we can use the MySQL LOCATE() function with proper syntax and good examples. Introduction of MySQL LOCATE() function LOCATE() function in MySQL is used for...

3 minutes read.