How to validate a Case result

Asked

Viewed 100 times

1

How to validate a Case result

I’m taking the interval between two dates in days, I want to validate if the result and more than 120 days, has a better way than I’m doing?

DECLARE @DataInicioVigencia datetime; -- ='2018-11-30T00:00:00'; 
DECLARE @DataInicioVigenciaProgramada datetime = '2018-11-30T00:00:00'; 
DECLARE @DataAdmissao       datetime = '2018-07-01T00:00:00';  

select
   CASE 
     WHEN isdate(@DataInicioVigencia) = 1 AND 
          (  ABS(DATEDIFF(day,@DataInicioVigencia,@DataAdmissao)) > 120 ) then 1
     WHEN isdate(@DataInicioVigenciaProgramada) = 1 AND 
          ( ABS(DATEDIFF(day,@DataInicioVigenciaProgramada,@DataAdmissao)) > 125 ) then 2
   ELSE 3
   END as 'PRAZO'
from FUNCIONARIOS

--mostrar o cálculo
select ABS(DATEDIFF(day,@DataInicioVigencia,@DataAdmissao))
select ABS(DATEDIFF(day,@DataInicioVigenciaProgramada,@DataAdmissao))
  • What’s the problem? The CASE you use is not returning correct result?

  • You sure need that ABS?

  • I wonder if you have a better option to do what I need

  • @Marconi, the value may have come negative, so the ABS

  • That one ABS can give rise to wrong results, since 120 more is quite different from 120 days less! The ISDATE also doesn’t seem necessary because if the guy is DATETIME validation always gives true (unless NULL, but then you can do ISNULL to validate). But in relation to the CASE, I don’t see any problem.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.