Re: any with the output of coalesce

From: "George Pavlov" <gpavlov(at)mynewplace(dot)com>
To: "SunWuKung" <Balazs(dot)Klein(at)gmail(dot)com>, <pgsql-general(at)postgresql(dot)org>
Subject: Re: any with the output of coalesce
Date: 2007-09-25 00:03:01
Message-ID: 8C5B026B51B6854CBE88121DBF097A86012C0F02@ehost010-33.exch010.intermedia.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

> I am trying to create an expression which
> - always yield true if the incomming array is NULL
> - yields true if a given value is in the array, otherwise yields false
>
> I thought this should work:
> Select 'target'=ANY(COALESCE('{indata1, indata2}','{target}'))
>
> but I get an ERROR: op ANY/ALL (array) requires array on right side
> Can somebody tell me what I am doing wrong and how to do this right?

Read up on array input syntax
(http://www.postgresql.org/docs/current/static/arrays.html). What you
are giving as input to your coalesce above are two strings, not two
arrays. Try this:

select 'target' = any(coalesce('{"indata1","target"}'::text[],
'{"target"}'::text[]));

I personally find the ARRAY syntax more intuitive:

select 'target' = any(coalesce(array['indata1','indata2'],
array['target']));

This will work as i think you expect it to. Note that array behavior
with respect to nulls changed with 8.2 so whether "incomming array is
NULL" will differ depend on your version.

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Jaime Casanova 2007-09-25 00:56:14 Re: AUXILIO!!!! CONSULTA SOBRE CURSORES HELP!!! ABOUT CURSORS
Previous Message Josh Trutwin 2007-09-24 23:51:56 table column reordering