Thread: Casting integer to boolean

Casting integer to boolean

From
Bhuvan A
Date:
Hi,

I am using postgresql 7.2.1.

How do i cast an integer value to boolean? I did try the below sequence of
SQLs and was little bit confused, by the way it behaves. It casts the
integer value to boolean in one case but not ever again.

bhuvan=> SELECT count(*)::int::boolean from my_table;
ERROR:  Cannot cast type 'integer' to 'boolean'
bhuvan=> -- The SQL similar to the above SQL is my requirement
bhuvan=> SELECT true where (1);
ERROR:  WHERE clause must return type boolean, not type integer
bhuvan=> SELECT true where (1::boolean);bool
------t
(1 row)

bhuvan=> SELECT true where (1::int::boolean);
ERROR:  Cannot cast type 'integer' to 'boolean'
bhuvan=>

I donot know whether i am wrong or its a bug. I request someone to correct
me if i am wrong or please suggest me the right way to cast an integer to
boolean as, returning true for non-zero value, false otherwise.

regards, 
bhuvaneswaran




Re: Casting integer to boolean

From
Tod McQuillin
Date:
On Fri, 16 Aug 2002, Bhuvan A wrote:

> How do i cast an integer value to boolean?

You can always do something like this:

select not count(*) = 0 from my_table;

Basically, for any integer i, convert to boolean with:  not i = 0
-- 
Tod McQuillin



Re: Casting integer to boolean

From
"Christopher Kings-Lynne"
Date:
> select not count(*) = 0 from my_table;
> 
> Basically, for any integer i, convert to boolean with:  not i = 0

Or i != 0 of course...

Chris