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?
– José Diz
You sure need that ABS?
– Marconi
I wonder if you have a better option to do what I need
– Harry
@Marconi, the value may have come negative, so the ABS
– Harry
That one
ABScan give rise to wrong results, since 120 more is quite different from 120 days less! TheISDATEalso doesn’t seem necessary because if the guy isDATETIMEvalidation always givestrue(unlessNULL, but then you can doISNULLto validate). But in relation to theCASE, I don’t see any problem.– João Martins