I have a sql query that I can not get to work correctly
Here is a simplified version of the query.
select * from Permit
inner join BMP on Permit.PermitNumber = BMP.PermitNumber
left join BMPInspection as BI on Permit.PermitNumber = BI.PermitNumber and BMP.BMPNumber = BI.BMPNumber
where Permit.PermitNumber = 'S002552'
AND ( ( @StartDate IS NULL
AND @EndDate IS NULL )
OR ( BI.dtActionDate > Dateadd(day, -1, @StartDate)
AND BI.dtActionDate < Dateadd(day, 1, @EndDate) )
OR ( BI.dtActionDate > Dateadd(day, -1, @StartDate)
AND @EndDate IS NULL )
OR ( @StartDate IS NULL
AND BI.dtActionDate < Dateadd(day, 1, @EndDate) ) )
The desired behavior is
- Return only records in the date range when start date and end date are specified
- Return NULL dates or less than end date when start date is NULL and end date has a date
- Return NULL or Greater than start date when start date has a date and end date is null
- Return only records with a NULL date when both start date and end date are NULL
Right now all I can get is is the date range or all records NULLS and valid dates.
For this condition:
Change:
to