CARDINALITY
The CARDINALITY function returns a value representing the number of elements of an array.
- array-expression
- The expression can be either an SQL variable or parameter of an array data type, or a cast specification of a parameter marker to an array data type.
The value returned by the CARDINALITY function is the highest array index for which the array has an assigned element. This includes elements that have been assigned the null value.
The result of the function is BIGINT. The function returns 0 if the array is empty. The result can be null; if the argument is null, the result is the null value.
Example
Assume that array type PHONENUMBERS and array variable RECENT_CALLS are defined as follows:
CREATE TYPE PHONENUMBERS AS INTEGER ARRAY[50];
DECLARE RECENT_CALLS PHONENUMBERS;
RECENT_CALLS contains
three elements. The following SET statement assigns the number of
calls that have been stored in the array so far to SQL variable HOWMANYCALLS:
SET HOWMANYCALLS = CARDINALITY(RECENT_CALLS)
After the statement executes, HOWMANYCALLS contains 3.