- publishing free software manuals
The PostgreSQL 9.0 Reference Manual - Volume 1A - SQL Language Reference
by The PostgreSQL Global Development Group
Paperback (6"x9"), 454 pages
ISBN 9781906966041
RRP £14.95 ($19.95)

Sales of this book support the PostgreSQL project! Get a printed copy>>>

7.10 Enum Support Functions

For enum types (described in section 6.7 Enumerated Types), there are several functions that allow cleaner programming without hard-coding particular values of an enum type. These are listed in Table 7-29. The examples assume an enum type created as:

CREATE TYPE rainbow AS ENUM ('red', 'orange', 'yellow', 
  'green', 'blue', 'purple');
Table 7-29: Enum Support Functions
Function Description
enum_first(anyenum) Returns the first value of the input enum type
e.g. enum_first(null::rainbow) => red
enum_last(anyenum) Returns the last value of the input enum type
e.g. enum_last(null::rainbow) => purple
enum_range(anyenum) Returns all values of the input enum type in an ordered array
e.g. enum_range(null::rainbow) => {red,orange,yellow,green,blue,purple}
enum_range(anyenum, anyenum) Returns the range between the two given enum values, as an ordered array. The values must be from the same enum type. If the first parameter is null, the result will start with the first value of the enum type. If the second parameter is null, the result will end with the last value of the enum type.
e.g. enum_range('orange'::rainbow, 'green'::rainbow) => {orange,yellow,green}
enum_range(NULL, 'green'::rainbow) {red,orange,yellow,green}
enum_range('orange'::rainbow, NULL) {orange,yellow,green,blue,purple}

Notice that except for the two-argument form of enum_range, these functions disregard the specific value passed to them; they care only about its declared data type. Either null or a specific value of the type can be passed, with the same result. It is more common to apply these functions to a table column or function argument than to a hardwired type name as suggested by the examples.

ISBN 9781906966041The PostgreSQL 9.0 Reference Manual - Volume 1A - SQL Language ReferenceSee the print edition