Re: Need help with embedded CASEs - Mailing list pgsql-sql

From Peter Eisentraut
Subject Re: Need help with embedded CASEs
Date
Msg-id Pine.LNX.4.30.0111072102530.835-100000@peter.localdomain
Whole thread Raw
In response to Need help with embedded CASEs  (Denis Bucher <dbucher@niftycom.com>)
List 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
ELSETIME '00:00' END       AS time_iftoday;
 

-- 
Peter Eisentraut   peter_e@gmx.net



pgsql-sql by date:

Previous
From: Tom Lane
Date:
Subject: Re: RIGHT JOIN is only supported with mergejoinable join conditions
Next
From: Roberto Mello
Date:
Subject: ISO SQL 92/99 paper?