Re: Inserting boolean types as an alias? - Mailing list pgsql-general

From David Johnston
Subject Re: Inserting boolean types as an alias?
Date
Msg-id 1386033537948-5781337.post@n5.nabble.com
Whole thread Raw
In response to Re: Inserting boolean types as an alias?  (John R Pierce <pierce@hogranch.com>)
Responses Re: Inserting boolean types as an alias?
List pgsql-general
John R Pierce wrote
> On 12/2/2013 3:46 PM, Nick wrote:
>> Hello I am new to this site and also a student. I am working on an
>> assignment
>> and was wondering if there is a way to make an alias for a boolean? For
>> example when I am making a table called club_games and it must contain
>> the
>> memberID, gameID, count, and status as column fields. When I enter the
>> data
>> for status I want it to say either "unavailable" for false and
>> "available"
>> for true. but I don't know how to do that.
>
> you could use an enum, or you could use a case statement on your query,
> like,
>
> SELECT memberID, gameID, hardwareID, count, case when status then
> 'available' else 'unavailable' end FROM club_Games

Not sure an enum would work since the source table has the "status" column
defined as boolean...

Condition conversion requires the use of the "CASE" expression.  One point
not made is that the column allows for NULL.  I like to avoid 3-value logic
thinking when possible so I'd suggest:

CASE WHEN status IS NULL THEN 'Unknown'
         WHEN status = true THEN 'Available'
         ELSE 'Unavailable'
END::varchar AS status_description

Then, if you need the logic elsewhere put that into a SQL function so you
can change the words without having to change every location the conversion
may be used.

You may also want a "description_to_status" function to perform the inverse.

David J.





--
View this message in context:
http://postgresql.1045698.n5.nabble.com/Inserting-boolean-types-as-an-alias-tp5781328p5781337.html
Sent from the PostgreSQL - general mailing list archive at Nabble.com.


pgsql-general by date:

Previous
From: bricklen
Date:
Subject: Re: Mismatched pg_class.reltuples between table and primary key
Next
From: John R Pierce
Date:
Subject: Re: Inserting boolean types as an alias?