PostgreSQL enum_first() Function
The PostgreSQL enum_first()
function returns the first enum value of the enum type specified by the parameter.
enum_first()
Syntax
Here is the syntax of the PostgreSQL enum_first()
function:
enum_first(enum_value ANYENUM) -> ANYENUM
Parameters
enum_value
-
Required. An enumeration value. Typically, a null value is passed in.
Return value
The PostgreSQL enum_first()
function returns the first enum value of the enum type specified by the parameter.
enum_first()
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_first()
function to return the first enum value of my_number
:
SELECT enum_first(null::my_number);
enum_first
------------
one
Here, we provide a null parameter null::my_number
of type my_number
for the enum_first()
function, and the enum_first()
function returns the my_number
first enum value one
in .
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_first('one'::my_number),
enum_first('two'::my_number),
enum_first('three'::my_number),
enum_first('four'::my_number),
enum_first('five'::my_number),
enum_first('six'::my_number),
enum_first('seven'::my_number),
enum_first('eight'::my_number),
enum_first('nine'::my_number),
enum_first('ten'::my_number);
-[ RECORD 1 ]---
enum_first | one
enum_first | one
enum_first | one
enum_first | one
enum_first | one
enum_first | one
enum_first | one
enum_first | one
enum_first | one
enum_first | one
Here, we pass in all enumeration values โโof my_number
type , and the enum_first()
function returns the first enumeration value one
in my_number
.