【问题标题】:SQL IIF Isnull and Update set = 0SQL IIF Isnull 和更新集 = 0
【发布时间】:2018-04-11 23:39:20
【问题描述】:

我的推荐人是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

对于我使用的任何方法的任何建议都将不胜感激。谢谢!

【问题讨论】:

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


    【解决方案1】:
    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

    【讨论】:

    • 谢谢!这确实很有意义!将在 54 秒内接受您的回答。
    猜你喜欢
    • 2021-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多