我有一个无法正常工作的SQL查询
以下是查询的简化版本。
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) ) )
期望的行为是
现在我能得到的是日期范围或所有记录NULLS和有效日期。
答案 0 :(得分:0)
对于这种情况:
仅在开始日期和结束日期时返回具有NULL日期的记录 是NULL
变化:
( @StartDate IS NULL AND @EndDate IS NULL )
到
( @StartDate IS NULL AND @EndDate IS NULL AND BI.dtActionDate IS NULL)