Re: Conditional on Select List

From: Sam Mason <sam(at)samason(dot)me(dot)uk>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: Conditional on Select List
Date: 2008-05-13 16:04:17
Message-ID: 20080513160417.GD1657@frubble.xen.chris-lamb.co.uk
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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

In response to

Browse pgsql-general by date

  From Date Subject
Next Message J. Manuel Velasco - UBILIBET 2008-05-13 16:05:48 Re: how can i get initdb
Previous Message Joshua D. Drake 2008-05-13 16:03:26 Re: Conditional on Select List