将Null转换为0

时间:2015-10-20 16:36:48

标签: sql-server

我正在对我的数据库运行此查询,并且它在脚本上按月和周返回null;但是,当我为年份总计运行时,不会返回null。

一年中的那个星期和月份不起作用的是什么?我想我完全拙劣的代码,但无法找到。

周:

declare @today datetime = '2015-10-15' 
set nocount on; 

select [Week Total] = convert(decimal(18,2), sum(isnull([Total],0)))
from [Accounting].[dbo].[Pay] 
where [AccountingDate] <= @today and 
[AccountingDate] > dateadd(week, -1, @today);

月:

declare @today datetime = '2015-10-15'
set nocount on; 

select [Month Total] = convert(decimal(18,2), sum(isnull([Total],0)))
from [Accounting].[dbo].[Pay] 
where [AccountingDate] <= @today and 
[AccountingDate] > dateadd(month, -1, @today);

年份:

declare @today datetime = '2015-10-15'
set nocount on;

select [Year Total] = convert(decimal(18,2), sum(isnull([Total],0)))
from [Accounting].[dbo].[Pay]
where [AccountingDate] <= @today and
[AccountingDate] > dateadd(year, -1, @today);

进行更改以显示一致性。

0 个答案:

没有答案
相关问题