How SQLite Length() Works

The SQLite length() function returns the number of characters in a string, number, or blob.

If there are any NUL characters, it returns the number of characters before the first NUL character.

Syntax

The syntax goes like this:

length(X)

Where X is the value that you want the length of.

Example

Here’s a basic example to demonstrate.

SELECT length('Rainbow');

Result:

7

Numeric Values

If the argument is numeric, then it returns the length of a string representation of the value.

SELECT length(789);

Result:

3

Any fractional part is also included (including the decimal place).

SELECT length(789.14);

Result:

6

A Database Example

Here’s an example of using length() on a column retrieved from a database.

SELECT 
  ProductName,
  length(ProductName)
FROM Products;

Result:

ProductName    length(ProductName)
-------------  -------------------
Widget Holder  13                 
Blue Widget    11                 
Red Widget     10                 
Green Widget   12                 
Widget Stick   12                 
Foo Cap        7                  

NULL Arguments

If the argument is NULL, then the result is NULL.

SELECT length(NULL);

Result:

 

(This is intentionally blank because the result was NULL).

Blobs

If the argument is a BLOB, then length() returns the number of bytes in the BLOB.

Here’s an example that uses a BLOB literal:

SELECT length(x'1234');

Result:

 2