SQL Length Function

Length Function in SQL is used to return the length of a column, string or variable in bytes or characters.

SQL Length Function

SQL Length Function

     

Length Function in SQL is used to return the length of a column, string or variable in bytes or characters.

Understand with Example

The Tutorial illustrate an example from SQL Length Function. To understand it, we create a table Stu_Table. The create table statement is used to create table 'Stu_Table' with the specific table attribute like column name, data type etc.

Create Table Stu_Table

create table Stu_Table(Stu_Id varchar(2), 
Stu_Name varchar(15), Stu_Address varchar(20));

Insert data into Stu_Table

The insert into adds the records or rows to the table 'Stu_Table'.

SQL statement to insert data into table:

insert into Stu_Table values(1,'komal','d 16 sec 3 rohani');
insert into Stu_Table values(2,'ajay','c 224 sec 19 nodia');
insert into Stu_Table values(3,'rakesh','c 225 sec 120 nodia');
insert into Stu_Table values(4,'santosh','a 124 sec 126 nodia');
insert into Stu_Table values(5,'tanuj','a 32 sec 26 nodia');

Stu_Table

+--------+----------+---------------------+
| Stu_Id | Stu_Name | Stu_Address         |
+--------+----------+---------------------+
| 1      | komal    | d 16 sec 3 rohani   |
| 2      | ajay     | c 224 sec 19 nodia  |
| 3      | rakesh   | c 225 sec 120 nodia |
| 4      | santosh  | a 124 sec 126 nodia |
| 5      | tanuj    | a 32 sec 26 nodia   |
+--------+----------+---------------------+

Query

The length(stu_address) helps you to find the length of column stu_address in the table Stu_Table.

select stu_id,stu_name,stu_address,
length(stu_address)from stu_table;

Result

+--------+----------+---------------------+---------------------+
| stu_id | stu_name | stu_address         | length(stu_address) |
+--------+----------+---------------------+---------------------+
| 1      | komal    | d 16 sec 3 rohani   | 17                  |
| 2      | ajay     | c 224 sec 19 nodia  | 18                  |
| 3      | rakesh   | c 225 sec 120 nodia | 19                  |
| 4      | santosh  | a 124 sec 126 nodia | 19                  |
| 5      | tanuj    | a 32 sec 26 nodia   | 17                  |
+--------+----------+---------------------+---------------------+