Thread: BUG REPORT: Unable to cast boolean::smallint

BUG REPORT: Unable to cast boolean::smallint

From
James Pittman
Date:
It does not seem possible to be able to cast a boolean to a smallint.

Whilst the following is possible:

SELECT true::int;

 int4
------
    1
(1 row)


The following is not (as of 9.4beta3)

SELECT true::smallint;
ERROR:  cannot cast type boolean to smallint


It would seem logically consistent that both cases would hold true


Apologies if this is more of a 'feature request' than a bug.





Kind regards,


James

Re: BUG REPORT: Unable to cast boolean::smallint

From
Tom Lane
Date:
James Pittman <j.pittman@travelaudience.com> writes:
> It does not seem possible to be able to cast a boolean to a smallint.

Well, there are also not direct casts provided from boolean to bigint,
numeric, real, or double precision ;-).  I'm not terribly excited about
this.

            regards, tom lane

Re: BUG REPORT: Unable to cast boolean::smallint

From
David G Johnston
Date:
Tom Lane-2 wrote
> James Pittman <

> j.pittman@

> > writes:
>> It does not seem possible to be able to cast a boolean to a smallint.
>
> Well, there are also not direct casts provided from boolean to bigint,
> numeric, real, or double precision ;-).  I'm not terribly excited about
> this.

So why is there one for Boolean to int?

To the OP, why do you think you want one?

While I haven't used it much converting Boolean to zero/one for use in a
math formula is a known use case though likely one where the difference
between int and smallint would probably be marginalized.

David J.



--
View this message in context:
http://postgresql.1045698.n5.nabble.com/BUG-REPORT-Unable-to-cast-boolean-smallint-tp5825768p5825784.html
Sent from the PostgreSQL - bugs mailing list archive at Nabble.com.

Re: BUG REPORT: Unable to cast boolean::smallint

From
Marko Tiikkaja
Date:
On 11/5/14 5:31 PM, David G Johnston wrote:
> Tom Lane-2 wrote
>> Well, there are also not direct casts provided from boolean to bigint,
>> numeric, real, or double precision ;-).  I'm not terribly excited about
>> this.
>
> So why is there one for Boolean to int?

That can be useful for e.g.  CHECK (bool1::int + bool2::int + bool3::int
= 1).


.marko