CARDINALITY function

Start of changeThe CARDINALITY function returns the number of elements in a list.End of change

SYNTAX

Start of changeCARDINALITY returns an integer value giving the number of elements in the list specified by ListExpression. End of change

Start of changeA common use of this function is to determine the number of fields in a list before iterating over them.End of change

Start of change

Examples

-- Determine the number of F1 fields in the message.
-- Note that the [ ] are required   
DECLARE CountF1 INT CARDINALITY(OutputRoot.XML.Data.Source.F1[]);
-- Determine the number of fields called F1 with the value 'F12' in the message.
-- Again note that the [ ] are required
DECLARE CountF1F12 INT     
  CARDINALITY(SELECT F.* FROM OutputRoot.XML.Data.Source.F1[] AS F 
              where F = 'F12');
-- Use the value returned by CARDINALITY to refer to a specific element 
-- in a list or array:
-- Array indices start at 1, so this example refers to the third-from-last
-- instance of the Item field
Body.Invoice.Item[CARDINALITY(Body.Invoice.Item[]) - 2].Quantity
End of change
Related concepts
ESQL overview
Related tasks
Developing ESQL
Related reference
Syntax diagrams: available types
ESQL list functions