SQLite | SQLite Functions | Returning the Number of Characters in a String (length Function)

The length function returns the number of characters in a string. This article explains how to use the function.

Using the length Function

The length function obtains the length of a string. Its syntax is as follows.

length(string)

It returns the number of characters in the supplied string. For a BLOB value, it returns the number of bytes. You can pass either a string directly or a column name to return the number of characters in the value stored in that column.

Let’s try an example. First, create the following table.

create table product (id integer, name_eng text, name_kor text);
sqlite> create table product (id integer, name_eng text, name_kor text);
sqlite> 

Add the following data with INSERT statements.

insert into product values (1, 'Apple', '사과');
insert into product values (2, 'Car', '자동차');
insert into product values (3, 'Television', '텔레비전');
insert into product values (4, 'Mobile', '휴대');
sqlite> insert into product values (1, 'Apple', '사과');
sqlite> insert into product values (2, 'Car', '자동차');
sqlite> insert into product values (3, 'Television', '텔레비전');
sqlite> insert into product values (4, 'Mobile', '휴대');
sqlite> 

Use length to retrieve the number of characters in the values stored in the name_eng and name_kor columns.

select name_eng, length(name_eng), name_kor, length(name_kor) from product;
sqlite> select name_eng, length(name_eng), name_kor, length(name_kor) from product;
name_eng    length(name_eng)  name_kor    length(name_kor)
----------  ----------------  ----------  ----------------
Apple       5                 사과          2               
Car         3                 자동차         3               
Television  10                텔레비전        4               
Mobile      6                 휴대          2               
sqlite> 

The query returns the number of characters in each stored value.

You can also pass a string directly to length.

select length('Flower');
sqlite> select length('Flower');
length('Flower')
----------------
6               
sqlite>