I am trying to use use a few conditions to select records based on start and end dates. Basically I want anything without an end date or that starts in the given period or ends in the same given period. Any help or direction on how this might be accomplished would be very appreciated. I’m including the WHERE part of the query below and the parameter binding but can include more if needed.
$periodsCxn->bindParam(':PositionID',$PositionID);
$periodsCxn->bindParam(':Start',$firstPayPeriod);
$periodsCxn->bindParam(':End',$lastPayPeriod);
$periodsCxn->bindParam(':Start2',$firstPayPeriod);
$periodsCxn->bindParam(':End2',$lastPayPeriod);
WHERE
POSITION_PERIOD.PositionID = :PositionID AND
(
(
POSITION_PERIOD.EndDate = '1900-01-01'
OR
POSITION_PERIOD.EndDate IS NULL
)
OR
(
POSITION_PERIOD.StartDate BETWEEN :Start AND :End
OR
POSITION_PERIOD.EndDate BETWEEN :Start2 AND :End2
)
)
It surprises me that you would want any record without an end date, but be comparing start dates in other cases.
Typically, with such data, you want to know if a given period overlaps another period, even when that period might have no end date. The logic for the overlap is:
This assumes that the variables have valid dates.