PostgreSQL enum_last() Function

The PostgreSQL enum_last() function returns the last enum value of the enum type specified by the parameter.

enum_last() Syntax

Here is the syntax of the PostgreSQL enum_last() function:

enum_last(enum_value ANYENUM) -> ANYENUM

Parameters

enum_value

Required. An enumeration value. Typically, a null value is passed in.

Return value

The PostgreSQL enum_last() function returns the last enum value of the enum type specified by the parameter.

enum_last() Examples

First, let’s create an enum type my_number using the CREATE TYPE statement:

CREATE TYPE my_number AS ENUM (
    'one',
    'two',
    'three',
    'four',
    'five',
    'six',
    'seven',
    'eight',
    'nine',
    'ten'
);

Here, we created an enum type my_number, it includes ten numbers from one to ten.

Then, let’s use the PostgreSQL enum_last() function to return the last enum value of my_number:

SELECT enum_last(null::my_number);
 enum_last
-----------
 ten

Here, we provide a my_number null parameter of type null::my_number, and the enum_last() function returns the last enum value ten in my_number.

Note that you cannot omit ::my_number in null::my_number, or PostgreSQL will give an error.

Of course, you can also pass in any enumeration value of type my_number, such as:

SELECT
    enum_last('one'::my_number),
    enum_last('two'::my_number),
    enum_last('three'::my_number),
    enum_last('four'::my_number),
    enum_last('five'::my_number),
    enum_last('six'::my_number),
    enum_last('seven'::my_number),
    enum_last('eight'::my_number),
    enum_last('nine'::my_number),
    enum_last('ten'::my_number);
-[ RECORD 1 ]--
enum_last | ten
enum_last | ten
enum_last | ten
enum_last | ten
enum_last | ten
enum_last | ten
enum_last | ten
enum_last | ten
enum_last | ten
enum_last | ten

Here, we pass in all enumeration values โ€‹โ€‹of my_number type , and the enum_last() function returns the last enumeration value ten in my_number.