Re: Conditional on Select List - Mailing list pgsql-general

From Sam Mason
Subject Re: Conditional on Select List
Date
Msg-id 20080513160417.GD1657@frubble.xen.chris-lamb.co.uk
Whole thread Raw
In response to Conditional on Select List  (Fernando <fernando@ggtours.ca>)
List pgsql-general
On Tue, May 13, 2008 at 11:52:24AM -0400, Fernando wrote:
> Is it possible to do this?
>
> SELECT IF(COUNT(colname) > 0, TRUE, FALSE) AS colname FROM table;
>
> What I want is to return a boolean, but when I tried SELECT
> COUNT(colname)::BOOLEAN FROM table; it says it cannot cast bigint to
> boolean.

Why not just do something like this?

  SELECT COUNT(colname) > 0 AS colname FROM table;

If you want a real conditional statement, CASE is probably what you
want:

  SELECT CASE WHEN COUNT(colname) > 0 THEN TRUE ELSE FALSE END AS colname FROM table;

Note that the ELSE clause is executed when the expression evaluates to
either NULL or FALSE, but because COUNT never returns a NULL value it
doesn't matter here.  It's also possible to have multiple WHEN clauses.


  Sam

pgsql-general by date:

Previous
From: "Joshua D. Drake"
Date:
Subject: Re: Conditional on Select List
Next
From: "Adam Rich"
Date:
Subject: Re: Conditional on Select List