length

Returns the length of the input array.

Syntax

length( <array> )

Returns

The data type of the returned value is INTEGER.

Examples

Here is a simple example:

SELECT length([1, 2, 3]);
+-------------------+
| length([1, 2, 3]) |
+-------------------+
| 3                 |
+-------------------+

Here is a slightly more complex example, this time using array data type:

CREATE TABLE colors (v array);

VALUES
     ('[{"r":255,"g":12,"b":0},{"r":0,"g":255,"b":0},{"r":0,"g":0,"b":255}]'),
     ('[{"r":255,"g":128,"b":0},{"r":128,"g":255,"b":0},{"r":0,"g":255,"b":128},{"r":0,"g":128,"b":255},{"r":128,"g":0,"b":255},{"r":255,"g":0,"b":128}]')
SELECT parse_json($0)::array
INSERT INTO colors;

Retrieve the length for each array:

FROM colors SELECT length(v);
+-----------+
| length(v) |
+-----------+
| 3         |
| 6         |
+-----------+

Retrieve the last element of each array:

FROM colors SELECT v[length(v)-1];
+-------------------------+
| v[length(v) - 1]        |
+-------------------------+
| {"b":255,"g":0,"r":0}   |
| {"b":128,"g":0,"r":255} |
+-------------------------+