Casting integer to boolean

Started by Bhuvan Aover 23 years ago3 messagesbugs
Jump to latest
#1Bhuvan A
bhuvansql@linuxfreemail.com

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

#2Tod McQuillin
devin@spamcop.net
In reply to: Bhuvan A (#1)
Re: Casting integer to boolean

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

#3Christopher Kings-Lynne
chriskl@familyhealth.com.au
In reply to: Tod McQuillin (#2)
Re: Casting integer to boolean

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