Enum functions
Use these functions to list the values of an ENUM type, to get its first or last value, or to find the numeric code of a value. Write the ENUM type in full in each cast, as in 'ok'::ENUM('sad', 'ok', 'happy'), because you cannot create a named ENUM type.
enum_code function
Returns the numeric code that stands for enum in its ENUM type: the position of the value in the type's list of values, counting from 0. The first value of the type has the code 0, the second has the code 1, and so on. If enum is NULL, the result is NULL.
An ENUM type is a fixed, ordered list of string values. You write the type in a cast, as in CAST('ok' AS ENUM('sad', 'ok', 'happy')) or 'ok'::ENUM('sad', 'ok', 'happy'). ENUM values sort by their code, not alphabetically. Two values of the same ENUM type also compare by their code. A comparison between an ENUM value and a string literal compares the text instead, so CAST('ok' AS ENUM('sad', 'ok', 'happy')) < 'happy' is false.
Syntax
enum_code(enum)
Arguments
The enum_code function takes the following argument:
enum-
The value whose code you want, as a value of an
ENUMtype. The function does not accept aVARCHARvalue, so cast a string to theENUMtype first.
Return type
An unsigned integer type that fits the number of values in the ENUM type: UTINYINT for up to 255 values, USMALLINT for up to 65,535 values, and UINTEGER for more.
Examples
The following SQL query returns the code of the value happy, the third value of the type.
SELECT enum_code( 'happy'::ENUM('sad', 'ok', 'happy') ) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query casts strings to an ENUM type and sorts them by their code, which follows the order of the type, not the alphabet.
SELECT m AS mood, enum_code( CAST(m AS ENUM('sad', 'ok', 'happy')) ) AS code FROM (VALUES ('ok'), ('happy'), ('sad')) AS v(m) ORDER BY code
The query returns the following results.
| mood | code |
|---|---|
|
|
|
|
|
|
The following SQL query passes a NULL value of the ENUM type, so the result is NULL.
SELECT enum_code( NULL::ENUM('sad', 'ok', 'happy') ) AS result
The query returns the following result.
| result |
|---|
|
Considerations
You cannot create a named ENUM type with CREATE TYPE, so write the ENUM type in full in each cast.
A query result cannot hold an ENUM value. To return one, cast it to VARCHAR, as in CAST(. Hive tables cannot store enum AS VARCHAR)ENUM values either.
A cast of a string that is not one of the type's values fails the query. To get NULL instead, use TRY_CAST.
enum_first function
Returns the first value in the list of values of the ENUM type of enum, as a VARCHAR. The function uses only the type of enum, not its value, so the result is the same for every value of the type, including NULL. You usually pass NULL cast to the ENUM type.
To get the last value, use the enum_last function. To get every value, use the enum_range function.
Syntax
enum_first(enum)
Arguments
The enum_first function takes the following argument:
enum-
An expression of the
ENUMtype whose first value you want. Its value is not used, so it can beNULL.
Return type
VARCHAR
Examples
The following SQL query returns the first value of an ENUM type.
SELECT enum_first( NULL::ENUM('sad', 'ok', 'happy') ) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query passes the value happy. The function ignores the value and returns the first value of its type.
SELECT enum_first( 'happy'::ENUM('sad', 'ok', 'happy') ) AS result
The query returns the following result.
| result |
|---|
|
Considerations
Write the ENUM type in a cast, as in NULL::ENUM('sad', 'ok', 'happy'). You cannot create a named ENUM type with CREATE TYPE.
enum_last function
Returns the last value in the list of values of the ENUM type of enum, as a VARCHAR. The function uses only the type of enum, not its value, so the result is the same for every value of the type, including NULL. You usually pass NULL cast to the ENUM type.
To get the first value, use the enum_first function. To get every value, use the enum_range function.
Syntax
enum_last(enum)
Arguments
The enum_last function takes the following argument:
enum-
An expression of the
ENUMtype whose last value you want. Its value is not used, so it can beNULL.
Return type
VARCHAR
Examples
The following SQL query returns the last value of an ENUM type.
SELECT enum_last( NULL::ENUM('sad', 'ok', 'happy') ) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query passes the value sad. The function ignores the value and returns the last value of its type.
SELECT enum_last( 'sad'::ENUM('sad', 'ok', 'happy') ) AS result
The query returns the following result.
| result |
|---|
|
Considerations
Write the ENUM type in a cast, as in NULL::ENUM('sad', 'ok', 'happy'). You cannot create a named ENUM type with CREATE TYPE.
enum_range function
Returns a list of VARCHAR values that holds each value that the ENUM type of enum defines, in the order of the type. The function uses only the type of enum, not its value, so the result is the same for every value of the type, including NULL.
To get only the values between two values of the type, use the enum_range_boundary function.
Syntax
enum_range(enum)
Arguments
The enum_range function takes the following argument:
enum-
An expression of the
ENUMtype whose values you want. Its value is not used, so it can beNULL.
Return type
VARCHAR[]
Examples
The following SQL query returns every value of an ENUM type.
SELECT enum_range( NULL::ENUM('sad', 'ok', 'happy') ) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query counts the values of an ENUM type.
SELECT len( enum_range(NULL::ENUM('sad', 'ok', 'happy')) ) AS value_count
The query returns the following result.
| value_count |
|---|
|
Considerations
Write the ENUM type in a cast, as in NULL::ENUM('sad', 'ok', 'happy'). You cannot create a named ENUM type with CREATE TYPE.
enum_range_boundary function
Returns the values of an ENUM type from start through end, including both, in the order of the type, as a list of VARCHAR values. A NULL for start makes the list begin at the first value of the type. A NULL for end makes the list run through the last value of the type. If start comes after end in the type, the result is an empty list.
To get every value of the type, use the enum_range function.
Syntax
enum_range_boundary(start, end)
Arguments
The enum_range_boundary function takes the following arguments:
start-
The first value to return, as a value of the
ENUMtype, orNULLto start with the first value of the type. end-
The last value to return, as a value of the same
ENUMtype, orNULLto end with the last value of the type.
Return type
VARCHAR[]
Examples
The following SQL query returns the values from the first value of the type through happy.
SELECT enum_range_boundary( NULL, 'happy'::ENUM('sad', 'ok', 'happy') ) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query returns the values from ok through the end of the type, and the values from the start of the type through ok.
SELECT enum_range_boundary( 'ok'::ENUM('sad', 'ok', 'happy'), NULL ) AS from_ok, enum_range_boundary( NULL, 'ok'::ENUM('sad', 'ok', 'happy') ) AS to_ok
The query returns the following result.
| from_ok | to_ok |
|---|---|
|
|
The following SQL query passes start and end in the reverse order of the type, so the result is an empty list.
SELECT enum_range_boundary( 'happy'::ENUM('sad', 'ok', 'happy'), 'sad'::ENUM('sad', 'ok', 'happy') ) AS result
The query returns the following result.
| result |
|---|
|
Considerations
At least one argument must have the ENUM type, so that the function knows which type to use. If both arguments are an untyped NULL, or if they have different ENUM types, the query fails.
Write the ENUM type in a cast, as in 'ok'::ENUM('sad', 'ok', 'happy'). You cannot create a named ENUM type with CREATE TYPE.