我有一个类似于下面的SQL表:
Mon. Tues. Wed. Thur. Friday. Sat. Sun. TotalHours
1 3 4 3 6 NULL 8 NULL
4 NULL 3 4 5 0 0 NULL
我正在尝试通过对剩余“日期”列的值进行求和来更正每条记录的 TotalHours 值。
我尝试了以下内容:
UPDATE [TestDatabase].[dbo].[Timekeeping]
SET [TotalHours] = [Sunday] + Monday + Tuesday + Wednesday + THursday + Friday + Saturday
WHERE [TotalHours] is null
GO
查询没有错误,并返回受影响的行数,但是对“小时”值的更改永远不会更改。这是因为某些记录有空值吗?
答案 0 :(得分:3)
这是因为某些记录有空值吗?
是
使用isnull
函数,因为null + value为null:
UPDATE [TestDatabase].[dbo].[Timekeeping]
SET [TotalHours] = isnull([Sunday],0) + isnull(Monday,0) + isnull(Tuesday,0) + isnull(Wednesday,0) + isnull(THursday,0) + isnull(Friday,0) + isnull(Saturday,0)
WHERE [TotalHours] is null