Re: array weirdity

From: Michael Glaesemann <grzm(at)seespotcode(dot)net>
To: Sim Zacks <sim(at)compulab(dot)co(dot)il>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: array weirdity
Date: 2009-09-03 13:41:09
Message-ID: 969AC16C-F8D0-4F4F-A13D-36D0F3041357@seespotcode.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general


On Sep 3, 2009, at 9:19 , Sim Zacks wrote:

> How can these both be true? Is there a way to see if a value is not in
> an array?
>
>
> select 1229 <> any('{1220,0,0,1228,1229,1231,0,0,0}'::int[]) as result
>
> result
>
> -------
>
> t
>

Here you're comparing 1229 to each element in the array in turn, and
returning TRUE if ANY of those comparisons are TRUE. As 1229 <> 1220
(for example), it's TRUE.

> select 1229 = any('{1220,0,0,1228,1229,1231,0,0,0}'::int[]) as result
>
> result
>
> -------
>
> t

Again, you're comparing 1229 to each element in the array in turn,
returning TRUE if ANY of the comparisons are TRUE. AS 1229 is an
element in the array, the result is TRUE.

You're probably looking for ALL

SELECT 1229 <> ALL('{1220,0,0,1228,1229,1231,0,0,0}'::int[]) as result;
result
--------
f
(1 row)

or use NOT

SELECT NOT (1229 = ANY('{1220,0,0,1228,1229,1231,0,0,0}'::int[])) as
result;
result
--------
f
(1 row)

Michael Glaesemann
grzm seespotcode net

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Richard Broersma 2009-09-03 14:24:50 Re: [Q] optmizing postgres for 'single client' / many small queries
Previous Message Richard Huxton 2009-09-03 13:24:43 Re: array weirdity