×

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 CEIL() function

In this context, we will learn how we can use the MySQL CEIL () function with proper syntax and good examples. Introduction of MySQL CEIL() function Input is taken by the CEIL()...

3 minutes read.

MySQL Advance function

The advance function operates numerical values, string values, and data types.  The advance function converts, displays, and compares given values as per requirement. Here, you find out database, table, and...

4 minutes read.

MySQL Control flow function

The control flow function uses values or operands for logical operations. These functions can works on single and multiple conditions. The control flow function is based on the Boolean expression....

3 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 REVERSE() function

In this context, we will learn how we can use the MySQL REVERSE() function with proper syntax and good examples. Introduction of MySQL REVERSE() function This function could be used to reverse...

2 minutes read.

MySQL UPDATE JOIN

The UPDATE JOIN is a MySQL statement used to perform cross-table updates that means we can update one table using another table with the JOIN clause condition. The "update join"...

5 minutes read.

MySQL AVG function

This function works on numerical data type values. The average function shows the average value of the data set. If an average function returns a null value, then the row...

5 minutes read.

MySQL FIELD() function

In this context, we will learn how we can use the MySQL FIELD() function with proper syntax and good examples. Introduction of MySQL FIELD() function The index position of a mentioned value...

3 minutes read.

MySQL function

MySQL function is a piece of program to perform some specific task. Here we pass a parameter and then return a single value. MySQL function mainly supports string, numeric, conditional,...

9 minutes read.

MySQL Command line client Basic Queries

MySQL – command line client Basic Queries There are many queries to work with the MySQL command-line client. Today, we will explore MySQL basic queries and their function. The MySQL query...

8 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 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 LN() function

In this context, we will learn how we can use the MySQL LN() function with proper syntax and good examples. Introduction of MySQL LN() function For evaluation of the natural logarithm of...

2 minutes read.

MySQL SUM function

The SUM() function displays the total addition of the table values. It supports the arithmetic operation of the column values. This function does not return a null value. Here, the...

5 minutes read.

MySQL ROUND() function

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

3 minutes read.

MySQL View

Introduction MySQL View is a virtual table to create a clone of the base table. The View does not contain its values or data. MySQL View creates to connect more than...

12 minutes read.

MySQL GREATEST function

This statement displays the greatest values of the table. The GREATEST function returns a null value when the table contains a null value. The GREATEST function needs a minimum of...

2 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 DATE_FORMAT() function

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

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.