Select case with expression and setting the result of the else

0

I have a problem with a select case, I need to do a select of an expression and if this expression gives a negative result, it shows me the value, if it is positive the value has to be changed to zero, but I can not get ta giving error when changing the value to zero, and my alias to name the table tmb does not work, follow the query below:

select case 
when DATEDIFF(MINUTE,HR_ENTRADA,HR_SAIDA) - DATEDIFF(MINUTE,HR_SAIDA_ALMOCO,HR_ENTRADA_ALMOCO )  < 0 
then (select DATEDIFF(MINUTE,HR_ENTRADA,HR_SAIDA) - DATEDIFF(MINUTE,HR_SAIDA_ALMOCO,HR_ENTRADA_ALMOCO ) as saldo)
else
    set saldo = 0
end
from TB_REGISTRO_PONTO 
    
asked by anonymous 26.02.2018 / 16:50

1 answer

0

So I understand you are not sending the answer options for the same attribute, but for different attributes, tend with this code, or if possible make available a few lines of results for example.

select case 
when DATEDIFF(MINUTE,HR_ENTRADA,HR_SAIDA) - 
DATEDIFF(MINUTE,HR_SAIDA_ALMOCO,HR_ENTRADA_ALMOCO )  < 0 
then (select DATEDIFF(MINUTE,HR_ENTRADA,HR_SAIDA) - 
DATEDIFF(MINUTE,HR_SAIDA_ALMOCO,HR_ENTRADA_ALMOCO ))
else
    0
end as saldo
from TB_REGISTRO_PONTO 
    
26.02.2018 / 16:57