Sorts the values of the input variables in ascending order and returns the value that is ranked nth.

Syntax

ordinal(bigint <nth>, <var1>, <var2>[,...])

Parameters

  • nth: required. A value of the BIGINT type. This parameter specifies the position of the value that you want to return. If the value is null, null is returned.
  • var: required. Values of the BIGINT, DOUBLE, DATETIME, or STRING type. This parameter specifies the values that you want to sort.

Return value

  • The value that is ranked nth is returned. If implicit conversions are not performed, the return value is of the same data type as the input parameter.
  • If a data type conversion is performed among the DOUBLE, BIGINT, and STRING types, a value of the DOUBLE type is returned. If a data type conversion is performed between the STRING and DATETIME types, a value of the DATETIME type is returned. Implicit conversions of other data types are not allowed.
  • The value null is interpreted as the minimum value.

Examples

-- The value 2 is returned. 
select ordinal(3, 1, 3, 2, 5, 2, 4, 6);