Thread: simplify case statement with function?

simplify case statement with function?

From
Rory Campbell-Lange
Date:
I have a case statement that is quite long to write!

I'd be greteful to know if I can simplify this by using a function, and
if so how I would go about 1) defining it and 2) using it.

Thanks for any help.



select id, idtext, title,
    case when n_msgs > 0 then n_msgs else 0 end as n_ms,
case
when
    ceil((extract(epoch from current_timestamp) - extract(epoch from ts_modified))/ (60 *60)) < 2
then 1
when
    ceil((extract(epoch from current_timestamp) - extract(epoch from ts_modified))/ (60 *60)) < 4
then 4
when
    ceil((extract(epoch from current_timestamp) - extract(epoch from ts_modified))/ (60 *60)) < 8
then 8
when
    ceil((extract(epoch from current_timestamp) - extract(epoch from ts_modified))/ (60 *60)) < 24
then 24
else
    2000
end as hours


--
Rory Campbell-Lange
<rory@campbell-lange.net>
<www.campbell-lange.net>