Wine Too
Wine Too

Reputation: 4655

PostgreSQL, Replace by Enum

I have int type column with all values from 0 to -5.

Is it possible to make in query some "enum" that instead of integer value I get in result text "ZERO, ONE, TWO, THREE, FOUR, FIVE" based on values 0, -1, -2, -3, -4 and -5, without adding functions to PostgreSQL server and without changing data in table.

If do how that query should look like.

Upvotes: 0

Views: 658

Answers (1)

user330315
user330315

Reputation:

select case the_column
         when 0 then 'ZERO'
         when -1 then 'ONE'
         when -2 then 'TWO'
         when -3 then 'THREE'
         when -4 then 'FOUR'
         when -5 then 'FIVE'
       end as value_label
from the_table

SQLFiddle example: http://sqlfiddle.com/#!15/48b69/1

The above will return null in case any other value is stored in the_column. If you don't want that, you need to add an else part to the case to return some default value (e.g. unknown)

Upvotes: 1

Related Questions