James
James

Reputation: 13

how to use the result from a case statement in a calculation

I have a case statement that returns a date;

case when (ma.first_active_date is not null or ma.first_deemed_date is not null)
             then (least(nvl(ma.first_active_date,sysdate),nvl(ma.first_deemed_date,sysdate)))
             else (ma.status_date) end djf

How can i then calculate a count of years between the returned date and the sysdate?

Upvotes: 1

Views: 284

Answers (1)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726639

You should be able to simply plug in your case statement into an expression, without the djf alias, of course:

((case when (ma.first_active_date is not null or ma.first_deemed_date is not null)
             then (least(nvl(ma.first_active_date,sysdate),nvl(ma.first_deemed_date,sysdate)))
             else (ma.status_date) end) - sysdate) diff_from_sysdate

Upvotes: 0

Related Questions