数据为 Null 无法在 Null 值错误不断出现时调用此方法或属性
本文关键字:Null 调用 此方法 属性 错误 数据 | 更新日期: 2023-09-27 18:35:54
我不知道
为什么我总是收到错误数据为空。不能对 Null 值调用此方法或属性。
这是我的查询,它将获得 2016 年 2 月 18 日至 2016 年 2 月 23 日期间员工编号 1 的所有总加班时间的总和:
select sum(totalovertime) as totalovertime from timeinout where employeenumber = 1 and dateofin between 2016-02-18 and 2016-02-23
这是我的桌子:
employeenumber | totalovertime | dateofin
1 | 1 | 2016-02-19
1 | 1 | 2016-02-22
查询应返回 2 作为总超时,但错误不断出现
但是当我这样做时:
select sum(totalovertime) from timeinout where employeenumber = 1
此查询返回 2,这是正确的。
处理日期时,您需要用单引号将它们括起来。 否则,SQL 会将2016-02-18
解释为 2016 减 2 减 18。 将查询更改为
select sum(totalovertime) as totalovertime
from timeinout
where employeenumber = 1
and dateofin between '2016-02-18' and '2016-02-23'
尝试对 SUM 中的值使用 ISNULL()
。
SUM(ISNULL(totalovertime, 0))
这将检查总加班是否为 null,如果是,则将其替换为 0,然后 SUM 可以正确处理。
也有可能您的 DateOfIn 对于某些字段为 NULL,并且 BETWEEN 运算符抛出您的错误,不确定这是否可能(检查您的表数据!