Re: Need help with embedded CASEs

From: Peter Eisentraut <peter_e(at)gmx(dot)net>
To: Denis Bucher <dbucher(at)niftycom(dot)com>
Cc: PGSQL-SQL <pgsql-sql(at)postgresql(dot)org>
Subject: Re: Need help with embedded CASEs
Date: 2001-11-08 00:25:40
Message-ID: Pine.LNX.4.30.0111072102530.835-100000@peter.localdomain
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Denis Bucher writes:

> SELECT CASE WHEN '2001-11-07' = current_date THEN 't' ELSE 'f' END AS
> flag_today, CASE WHEN flag_today THEN current_time ELSE '00:00' END AS
> time_iftoday;
>
> Why doesn't it work ?

For one thing, flag_today is not a made boolean expression, so the second
CASE wouldn't even work on that account. Secondly, the "AS" aliases in
the select list aren't available as variables in the other items in the
select list.

A corrected version would be:

SELECT
CASE WHEN DATE '2001-11-07' = current_date THEN true ELSE false END
AS flag_today,
CASE WHEN DATE '2001-11-07' = current_date THEN current_time ELSE TIME '00:00' END
AS time_iftoday;

Or shorter:

SELECT
DATE '2001-11-07' = current_date AS flag_today,
CASE WHEN DATE '2001-11-07' = current_date THEN current_time ELSE TIME '00:00' END
AS time_iftoday;

--
Peter Eisentraut peter_e(at)gmx(dot)net

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Roberto Mello 2001-11-08 00:40:55 Re: Can't find docs on Postgresql.conf
Previous Message Jason Earl 2001-11-07 23:55:14 Re: performance tuning