简体   繁体   中英

SQL IIF Isnull and Update set = 0

My good reference is How to replace blank (null ) values with 0 for all records?

Goal : I have blank query output. Image0 I hope to print out 0 instead.

Problem : When I tried IIF Isnull , I get a prompt message box to enter the parameter value, which I do not want to see. 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"));

Here is the my query looks after running the SQL: Image2

Then I tried 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;

It gives me The SELECT statement includes a reserved word or an argument name that is misspelled or missing, or the punctuation is incorrect. error message: Image3

Any advice regarding any of the methods I used would be highly appreciated. Thank you!

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"))

Your query at execution time doesn't know what the column SumOfDollarsSold is. So you would either have to make that a subquery and test for a value or you can use the actual aggregation function in your IIf statement.

Or perhaps a better thing for display purposes would be the FORMAT() function but note then that the number would actually be a string. I don't remember access's implementation too well but it might be something like:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM