Problem with case when try to alter the query from siimple

Hello PL/SQL gurus and Experts.

I'm stuck with a question (can be simple), but not gettings all headsway.
select 365/day_num_yr Anul_Fact from Date where date_dt = trunc(sysdate -1) 
value returns is as - 1.46

need to write it in the form of case (I have to multiply it to some other outer query), I want to store variable ina and then use the same in an outside -.
but when use the following syntax, then it always returns 0 and not the same output returned by the query above without the scabbard.
select (case when date_dt =trunc(sysdate -1) then 365/day_num_yr else 0 END) Anul_Fact 
Help kindly, I appericate your time and effort in advance.

user555994 wrote:
Problem is that I do not get the output as 1.46, even the output is coming like the 0 only if using the query - next

select /*date_dt, sysdate - 1 prev_dt, trunc(sysdate - 1) trunc_prev_dt,*/
case when date_dt = trunc(sysdate -1) then  365/day_num_yr
else 0
end num_day
from date_dim;

Completely, which seems to be a question of DATA. Have you checked if the table contains data for DATE_DT = SYSDATE - 1? Can you check if the data stored do not have hours and Minutes stored?

Although I have provided examples of data, which is the same as the data in my main table and once I used the previous solution you provided and then also gives the result as same as those mentioned by you.

I think it's something like -

select date_dt, sysdate - 1 prev_dt, trunc(sysdate - 1) trunc_prev_dt,
case when date_dt = trunc(sysdate -1) then  366/day_num_yr where day_num_yr=(select day_num_yr from date_dim where date_dt = trunc(sysdate -1))
else 0
end num_day
from date_dim;

Once we get the day_num_yr then he deviding by 366/day_num_yr :(
but he does not like throwing an error ORA-95 - missing keyword

Yes, it does not work

-case when date_dt = trunc (sysdate-1) then 366/day_num_yr where day_num_yr = (select day_num_yr from the date_dim where date_dt = trunc (sysdate-1))

due to a syntax of alien.
I don't think that you really need. I already said, with the data in your Table, you will be having only * 1 * record with a Non - zero value. Thus, simply apply a filter to extract the corresponding record SYSDATE - 1 and you should get an output which is Non-zero. If you apply a where predicate, then would not need you a CASE statement. You can directly use something like below:

select date_dt, sysdate - 1 prev_dt, trunc(sysdate - 1) trunc_prev_dt,
       365 / day_num_yr num_day
  from t4
 where date_dt = trunc(sysdate - 1);

Published by: Jen K, September 7, 2012 16:00

Tags: Database

Similar Questions

Maybe you are looking for