The LENGTH()
function is used to return the number of characters in a string.
The syntax for this function is as follows:
LENGTH(string)
string
: This represents the string whose length is to be returned.The following code demonstrates how to use the LENGTH()
function in SQL:
CREATE TABLE Person (ID int,first_name varchar(50),last_name varchar (50),gender varchar(10));-- Insert dataINSERT INTO PersonVALUES (1,'Sharon', 'Peller','Female');INSERT INTO PersonVALUES (2,'Paul', 'Dons','Male');INSERT INTO PersonVALUES (3,'Ameera', 'Abedayo','Female');INSERT INTO PersonVALUES (4,'Maria', 'Elijah','Female');INSERT INTO PersonVALUES (5,'David', 'Hassan','Male');INSERT INTO PersonVALUES (6,'Niniola', 'Disu','Female');INSERT INTO PersonVALUES (8,'Joe', 'Smith','Male');-- QuerySELECT LENGTH(first_name) AS no_of_char, genderFROM Person;
In the code above:
Person
which has the columns id
, name
, and gender
.Person
table.LENGTH()
function to return the number of characters in the first_name
columns to form a new column called no_of_char
.