Skip to content

Feature request: retrieve integer index values from an ENUM column using SQL #5395

@evansd

Description

@evansd

I'd like to have the option to retrieve the underlying index values from an ENUM-typed column using SQL. Perhaps this could be done by explicitly casting the column to an int?

For instance, given the following setup code:

CREATE TYPE mood AS ENUM ('sad', 'ok', 'happy');
CREATE TABLE person (
    name text,
    current_mood mood
);
INSERT INTO person VALUES ('Moe','happy'), ('Dave', NULL), ('Larry', 'sad'), ('Curly, 'ok');

And this query:

SELECT
  name, CAST(current_mood AS INTEGER) AS mood_index
FROM
  person;

The results would look like:

name mood_index
Moe 2
Dave NULL
Larry 0
Curly 1

Apologies if there's already a way to do this which I've missed. And thanks so much for DuckDB! It's a staggeringly impressive piece of software.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions