Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8532519
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T09:44:02+00:00 2026-06-11T09:44:02+00:00

Is it possible cast enum to integer? Starting from 1 the first element

  • 0

Is it possible cast enum to integer? Starting from 1 the first element

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T09:44:03+00:00Added an answer on June 11, 2026 at 9:44 am

    While you can’t cast enum to integer as Catcall explained, you can use the PostgreSQL-specific and possibly not-version-to-version compatible pg_enum system catalog table to get an ordinal representation.

    regress=# CREATE TYPE happiness AS ENUM ('happy', 'very happy', 'ecstatic');
    
    regress=# select enumsortorder, enumlabel from pg_catalog.pg_enum 
    regress-# WHERE enumtypid = 'happiness'::regtype ORDER BY enumsortorder;
     enumsortorder | enumlabel  
    ---------------+------------
                 1 | happy
                 2 | very happy
                 3 | ecstatic
    (3 rows)
    

    This looks easy, but it isn’t. Observe:

    regress=# ALTER TYPE happiness ADD VALUE 'sad' BEFORE 'happy';
    regress=# ALTER TYPE happiness ADD VALUE 'miserable' BEFORE 'very happy';
    regress=# SELECT * FROM pg_enum ;
     enumtypid | enumsortorder | enumlabel  
    -----------+---------------+------------
        185300 |             1 | happy
        185300 |             2 | very happy
        185300 |             3 | ecstatic
        185300 |             0 | sad
        185300 |           1.5 | miserable
    (5 rows)
    

    From this you can see that enumsortorder provides ordering, but no fixed ‘distance’. If support for removing values from enums is ever added, it’ll likely create ‘holes’ in the sequence, too.

    To get the enum position you’ll need to use the row_number() window function to get the ordering, and the pg_typeof to get the oid (regtype) of the enum type. You need this to make sure that you return the right ordinal when there are multiple enums with the same label.

    This function does the job:

    CREATE OR REPLACE FUNCTION enum_to_position(anyenum) RETURNS integer AS $$
    SELECT enumpos::integer FROM (
            SELECT row_number() OVER (order by enumsortorder) AS enumpos,
                   enumsortorder,
                   enumlabel
            FROM pg_catalog.pg_enum
            WHERE enumtypid = pg_typeof($1)
        ) enum_ordering
        WHERE enumlabel = ($1::text);
    $$ LANGUAGE 'SQL' STABLE STRICT;
    

    Note:

    • It’s STABLE not IMMUTABLE, because adding (or if support in Pg is later added, removing) values from enums would change the ordering and break indexes relying on the ordering; so
    • You cannot use this in an index expression; and
    • It’s STRICT because it should return null for a null input

    You can now use this function to CREATE CAST for specific enums to integer. You cannot create a generic cast for all enums to integer, because the anyenum pseudo-type cannot be used for casts. For example, if I want to allow the demo happiness to be cast to integer, I would write:

    CREATE CAST (happiness AS integer) WITH FUNCTION enum_to_position(anyenum);
    

    after which I could successfully execute:

    regress=# SELECT ('happy'::happiness)::integer;
     int4 
    ------
        2
    (1 row)
    

    Note that this is probably an insane thing to do, is unsupported, and is quite likely a terrible idea. Your code must be aware that the ordinal values will change when you add or (if later supported) remove a value from the enum.

    Indexes created based on this cast (only possible if the function is defined immutable) will begin producing crazy and wrong results if you change the definition of the enum (except by appending new values to the end of it) because PostgreSQL believes you when you say a function is immutable. Don’t do that.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible (or even advisable) to cast the element retrieved from a for
Possible Duplicate: Cast string to enum with enum attribute I have a enum like
Is it possible to do the enum without having to do a cast? class
Is it possible to cast from an InputStream to an AudioInputStream? I want to
Is it possible to cast an object to the type returned from GetType() ?
I know it's possible to cast a list of items from one type to
Is it possible to cast an object from a base to a derived one?
Is it possible to cast an enum to another object? I have enumarated a
How can I cast a value from Enum1 to Enum 2 in Java? Here
Possible Duplicate: Implicit cast from char** to const char** Given the following code: void

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.