

# 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
<a name="athena-duckdb-functions-utility-enum-code"></a>

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
<a name="athena-duckdb-functions-utility-enum-code-syntax"></a>

```sql
enum_code({{enum}})
```

### Arguments
<a name="athena-duckdb-functions-utility-enum-code-arguments"></a>

The `enum_code` function takes the following argument:

**{{enum}}**  
The value whose code you want, as a value of an `ENUM` type. The function does not accept a `VARCHAR` value, so cast a string to the `ENUM` type first.

### Return type
<a name="athena-duckdb-functions-utility-enum-code-return-type"></a>

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
<a name="athena-duckdb-functions-utility-enum-code-examples"></a>

The following SQL query returns the code of the value `happy`, the third value of the type.

```sql
SELECT
    enum_code(
        'happy'::ENUM('sad', 'ok', 'happy')
    ) AS result
```

The query returns the following result.


| result | 
| --- | 
| `2` | 

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.

```sql
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 | 
| --- | --- | 
| `sad` | `0` | 
| `ok` | `1` | 
| `happy` | `2` | 

The following SQL query passes a `NULL` value of the `ENUM` type, so the result is `NULL`.

```sql
SELECT
    enum_code(
        NULL::ENUM('sad', 'ok', 'happy')
    ) AS result
```

The query returns the following result.


| result | 
| --- | 
| `NULL` | 

### Considerations
<a name="athena-duckdb-functions-utility-enum-code-considerations"></a>

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({{enum}} AS VARCHAR)`. Hive tables cannot store `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
<a name="athena-duckdb-functions-utility-enum-first"></a>

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](#athena-duckdb-functions-utility-enum-last). To get every value, use the [`enum_range` function](#athena-duckdb-functions-utility-enum-range).

### Syntax
<a name="athena-duckdb-functions-utility-enum-first-syntax"></a>

```sql
enum_first({{enum}})
```

### Arguments
<a name="athena-duckdb-functions-utility-enum-first-arguments"></a>

The `enum_first` function takes the following argument:

**{{enum}}**  
An expression of the `ENUM` type whose first value you want. Its value is not used, so it can be `NULL`.

### Return type
<a name="athena-duckdb-functions-utility-enum-first-return-type"></a>

`VARCHAR`

### Examples
<a name="athena-duckdb-functions-utility-enum-first-examples"></a>

The following SQL query returns the first value of an `ENUM` type.

```sql
SELECT
    enum_first(
        NULL::ENUM('sad', 'ok', 'happy')
    ) AS result
```

The query returns the following result.


| result | 
| --- | 
| `sad` | 

The following SQL query passes the value `happy`. The function ignores the value and returns the first value of its type.

```sql
SELECT
    enum_first(
        'happy'::ENUM('sad', 'ok', 'happy')
    ) AS result
```

The query returns the following result.


| result | 
| --- | 
| `sad` | 

### Considerations
<a name="athena-duckdb-functions-utility-enum-first-considerations"></a>

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
<a name="athena-duckdb-functions-utility-enum-last"></a>

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](#athena-duckdb-functions-utility-enum-first). To get every value, use the [`enum_range` function](#athena-duckdb-functions-utility-enum-range).

### Syntax
<a name="athena-duckdb-functions-utility-enum-last-syntax"></a>

```sql
enum_last({{enum}})
```

### Arguments
<a name="athena-duckdb-functions-utility-enum-last-arguments"></a>

The `enum_last` function takes the following argument:

**{{enum}}**  
An expression of the `ENUM` type whose last value you want. Its value is not used, so it can be `NULL`.

### Return type
<a name="athena-duckdb-functions-utility-enum-last-return-type"></a>

`VARCHAR`

### Examples
<a name="athena-duckdb-functions-utility-enum-last-examples"></a>

The following SQL query returns the last value of an `ENUM` type.

```sql
SELECT
    enum_last(
        NULL::ENUM('sad', 'ok', 'happy')
    ) AS result
```

The query returns the following result.


| result | 
| --- | 
| `happy` | 

The following SQL query passes the value `sad`. The function ignores the value and returns the last value of its type.

```sql
SELECT
    enum_last(
        'sad'::ENUM('sad', 'ok', 'happy')
    ) AS result
```

The query returns the following result.


| result | 
| --- | 
| `happy` | 

### Considerations
<a name="athena-duckdb-functions-utility-enum-last-considerations"></a>

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
<a name="athena-duckdb-functions-utility-enum-range"></a>

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](#athena-duckdb-functions-utility-enum-range-boundary).

### Syntax
<a name="athena-duckdb-functions-utility-enum-range-syntax"></a>

```sql
enum_range({{enum}})
```

### Arguments
<a name="athena-duckdb-functions-utility-enum-range-arguments"></a>

The `enum_range` function takes the following argument:

**{{enum}}**  
An expression of the `ENUM` type whose values you want. Its value is not used, so it can be `NULL`.

### Return type
<a name="athena-duckdb-functions-utility-enum-range-return-type"></a>

`VARCHAR[]`

### Examples
<a name="athena-duckdb-functions-utility-enum-range-examples"></a>

The following SQL query returns every value of an `ENUM` type.

```sql
SELECT
    enum_range(
        NULL::ENUM('sad', 'ok', 'happy')
    ) AS result
```

The query returns the following result.


| result | 
| --- | 
| `[sad, ok, happy]` | 

The following SQL query counts the values of an `ENUM` type.

```sql
SELECT
    len(
        enum_range(NULL::ENUM('sad', 'ok', 'happy'))
    ) AS value_count
```

The query returns the following result.


| value\_count | 
| --- | 
| `3` | 

### Considerations
<a name="athena-duckdb-functions-utility-enum-range-considerations"></a>

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
<a name="athena-duckdb-functions-utility-enum-range-boundary"></a>

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](#athena-duckdb-functions-utility-enum-range).

### Syntax
<a name="athena-duckdb-functions-utility-enum-range-boundary-syntax"></a>

```sql
enum_range_boundary({{start}}, {{end}})
```

### Arguments
<a name="athena-duckdb-functions-utility-enum-range-boundary-arguments"></a>

The `enum_range_boundary` function takes the following arguments:

**{{start}}**  
The first value to return, as a value of the `ENUM` type, or `NULL` to start with the first value of the type.

**{{end}}**  
The last value to return, as a value of the same `ENUM` type, or `NULL` to end with the last value of the type.

### Return type
<a name="athena-duckdb-functions-utility-enum-range-boundary-return-type"></a>

`VARCHAR[]`

### Examples
<a name="athena-duckdb-functions-utility-enum-range-boundary-examples"></a>

The following SQL query returns the values from the first value of the type through `happy`.

```sql
SELECT
    enum_range_boundary(
        NULL, 'happy'::ENUM('sad', 'ok', 'happy')
    ) AS result
```

The query returns the following result.


| result | 
| --- | 
| `[sad, ok, happy]` | 

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`.

```sql
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 | 
| --- | --- | 
| `[ok, happy]` | `[sad, ok]` | 

The following SQL query passes `start` and `end` in the reverse order of the type, so the result is an empty list.

```sql
SELECT
    enum_range_boundary(
        'happy'::ENUM('sad', 'ok', 'happy'),
        'sad'::ENUM('sad', 'ok', 'happy')
    ) AS result
```

The query returns the following result.


| result | 
| --- | 
| `[]` | 

### Considerations
<a name="athena-duckdb-functions-utility-enum-range-boundary-considerations"></a>

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`.