SQL length() function

SQL length() function:- We use this to display data from a table according to the length of the values stored in the table’s cell. For example, If I want to retrieve data with length 3 from a column then I will use query – SELECT * FROM table WHERE length(column) < 4;

This function applies for both string and number values.

Database Name– jai_school

CREATE DATABASE jai_school;

Create a table– result

CREATE TABLE result(students VARCHAR(50),marks INT(10));

Insert data in result table

INSERT INTO result(students,marks)VALUES("Ram",450);
INSERT INTO result(students,marks)VALUES("Rahim",200);
INSERT INTO result(students,marks)VALUES("Shyam",90);
INSERT INTO result(students,marks)VALUES("Anita",350);
INSERT INTO result(students,marks)VALUES("Raman",200);
INSERT INTO result(students,marks)VALUES("Nobita",70);
INSERT INTO result(students,marks)VALUES("Ravi",250);
INSERT INTO result(students,marks)VALUES("Aman",150);
INSERT INTO result(students,marks)VALUES("Ravina",500);

Table Image-

table-for-MIN-and-MAX

EXAMPLES


length() function for number value

SELECT * FROM result WHERE length(marks) < 3;

Query result-

length-function-for-number-value

length() function for string value

SELECT * FROM result WHERE length(students) < 4;

Query result-

length-function-for-string-value

Leave a Comment