SQL IIF Isnull和Update set = 0

时间:2017-10-30 21:41:02

标签: sql ms-access null ms-access-2010 ms-access-2007

我的好参考是How to replace blank (null ) values with 0 for all records?

目标:我有空白的查询输出。 Image0我希望打印出0。

问题:当我尝试IIF Isnull时,我会收到一个提示消息框,输入参数值,我不想看到。 Image1

PARAMETERS [BeginDate] DateTime, [EndDate] DateTime;
SELECT Sum(dbo_SO_SalesHistory.DollarsSold) AS SumOfDollarsSold, IIF(ISNULL([SumOfDollarsSold]), 0, [SumOfDollarsSold])
FROM dbo_SO_SalesHistory
WHERE (((dbo_SO_SalesHistory.InvoiceDate) Between [BeginDate] And [EndDate]) AND ((dbo_SO_SalesHistory.CustomerNo)="M"));

以下是运行SQL时的查询:Image2

然后我尝试了UPDATE

PARAMETERS [BeginDate] DateTime, [EndDate] DateTime;
SELECT Sum(dbo_SO_SalesHistory.DollarsSold) AS SumOfDollarsSold
UPDATE [dbo_SO_SalesHistory.DollarsSold] SET [SumOfDollarsSold] = 0
FROM dbo_SO_SalesHistory
WHERE (((dbo_SO_SalesHistory.InvoiceDate) Between [BeginDate] And [EndDate]) AND ((dbo_SO_SalesHistory.CustomerNo)="M")) AND [SumOfDollarsSold] IS NULL;

它向我显示The SELECT statement includes a reserved word or an argument name that is misspelled or missing, or the punctuation is incorrect.错误消息:Image3

对于我使用的任何方法的任何建议都将受到高度赞赏。谢谢!

1 个答案:

答案 0 :(得分:0)

PARAMETERS [BeginDate] DateTime, [EndDate] DateTime;
SELECT IIF(ISNULL(Sum(dbo_SO_SalesHistory.DollarsSold)),0,Sum(dbo_SO_SalesHistory.DollarsSold)) AS SumOfDollarsSold
FROM dbo_SO_SalesHistory
WHERE (((dbo_SO_SalesHistory.InvoiceDate) Between [BeginDate] And [EndDate]) AND ((dbo_SO_SalesHistory.CustomerNo)="M"))

您在执行时的查询不知道列SumOfDollarsSold是什么。因此,您必须将其设为subquery并测试值,或者您可以在IIf语句中使用实际的聚合函数。

或许用于显示目的的更好的事情是FORMAT()函数,但请注意该数字实际上是一个字符串。我不太清楚访问的实现,但它可能是这样的:

PARAMETERS [BeginDate] DateTime, [EndDate] DateTime;
SELECT FORMAT(Sum(dbo_SO_SalesHistory.DollarsSold)),0) AS SumOfDollarsSold
FROM dbo_SO_SalesHistory
WHERE (((dbo_SO_SalesHistory.InvoiceDate) Between [BeginDate] And [EndDate]) AND ((dbo_SO_SalesHistory.CustomerNo)="M"))

https://support.office.com/en-us/article/Format-Property-Number-and-Currency-Data-Types-ca77795e-b40d-4abb-b42f-daa0bb709620?ui=en-US&rs=en-US&ad=US