Skip to content
Related Articles

Related Articles

LENGTH() Function in MySQL

Improve Article
Save Article
  • Last Updated : 15 Feb, 2021
Improve Article
Save Article

LENGTH() :

This function in MySQL is used to find the string length which is of type bytes.

Features :

  • This function is used to find the string length which is of type bytes.
  • This function comes under String Functions.
  • This function accepts only one parameter namely string.
  • This function returns the length in bytes only.

Syntax :

LENGTH(string)

Parameters :

This method accepts only one parameter.

  • string – 
    A specified string whose length is to be counted.

Returns :

It returns the string length which is of type bytes.

Example-1 :

Using LENGTH() function and getting the output.

SELECT LENGTH("GEEKSFORGEEKS");

Output :

13

Example-2 :

Using LENGTH() function and finding 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 :

Using LENGTH() function and getting the length of the item given.

CREATE TABLE package099
(  
user_id int NOT NULL AUTO_INCREMENT,
item VARCHAR(10),
mrp int,
PRIMARY KEY(user_id)
);
INSERT package099(item, mrp)  
VALUES ('books', 350);

SELECT LENGTH(item) FROM package099;

Output :

5

Example-4 :

Using LENGTH() function and getting 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 :

This function is used to find the string length which is of type bytes.

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!