vendredi 19 août 2016

How are these nested DATE_ADD and IF conditions different?

I want to find a list of employees with working age between 1 and 5 years, including those with end of employment date. I used the following WHERE clause:

Condition 1

 WHERE PerBeginDate<=IF(PerEndDate IS NULL,DATE_ADD(Now(),INTERVAL -1 YEAR),DATE_ADD(PerEndDate,INTERVAL -1 YEAR))
  AND PerBeginDate>=IF(PerEndDate IS NULL,DATE_ADD(Now(),INTERVAL -5 YEAR),DATE_ADD(PerEndDate,INTERVAL -5 YEAR))

Condition 2

 WHERE PerBeginDate<=DATE_ADD(IF(PerEndDate IS NULL,Now(),PerEndDate),INTERVAL -1 YEAR)
  AND PerBeginDate>=DATE_ADD(IF(PerEndDate IS NULL,Now(),PerEndDate),INTERVAL -5 YEAR)

Condition 2 does not return anyone who does not have an end date. Why?

Aucun commentaire:

Enregistrer un commentaire