为日期SQL返回null

时间:2017-08-10 00:53:36

标签: sql sql-server

我有以下查询:

select EL.Datetimestart, E.Fullname, VisaExpiryDate, E.EmployeeStatusCode
from EmployeeLog as EL
inner join Employee as E on E.Employeekey = EL.EmployeeKey
where DateTimeStart >= Getdate() -1 and VisaExpiryDate <=GetDate() and E.EmployeeStatusCode <> 'x' 
order by VisaExpiryDate

目前,这将退回签证过期且当天工作的员工,

VisaExpiryDate - 2017-08-08 00:00:00.000
DateTimeStart - 2017-08-10 08:00:03.090
FullName - Rypsuke
EmployeeStatusCode - C

检查签证信息并将其插入系统,这可能需要几天时间,因此新员工会收到“空白”信息。因为该字段留空。

我如何返回这些空值,以查看尚未检查签证的人和签证过期的人?

感谢您的帮助 尼克

2 个答案:

答案 0 :(得分:2)

使用IS NULL

select EL.Datetimestart, E.Fullname, VisaExpiryDate, E.EmployeeStatusCode
from EmployeeLog as EL
inner join Employee as E on E.Employeekey = EL.EmployeeKey
where DateTimeStart >= Getdate() -1 and (VisaExpiryDate <=GetDate() OR VisaExpiryDate IS NULL)and E.EmployeeStatusCode <> 'x' 
order by VisaExpiryDate

答案 1 :(得分:1)

如果我理解正确,您只需在where子句中添加条件:

select EL.Datetimestart, E.Fullname, VisaExpiryDate, E.EmployeeStatusCode
from EmployeeLog EL inner join
     Employee E
     on E.Employeekey = EL.EmployeeKey
where DateTimeStart >= Getdate() - 1 and
      (VisaExpiryDate <= GetDate() or VisaExpiryDate is null) and
      E.EmployeeStatusCode <> 'x' 
order by VisaExpiryDate;