简体   繁体   English

检查 MS Access Query 中的空值

[英]Check null value in MS Access Query

In SQL Server we can use IsNull() function to check whether expression value is null or not.在 SQL Server 中,我们可以使用 IsNull() 函数来检查表达式值是否为空。 For ex.例如。

Select IsNull(sum(amount),0) as TotalAmount
  From Payments 

Likewise is there any function in MS Access Query to check the null?同样,MS Access Query 中是否有任何功能可以检查空值? I need the same statement to be executed in MS Access Query.我需要在 MS Access Query 中执行相同的语句。

Can anybody tell me the replacement for IsNull() in MS Access?有人能告诉我 MS Access 中IsNull()的替代品吗?

Using Jet/ACE your query can be re-written as:使用 Jet/ACE,您的查询可以重写为:

SELECT IIf(Sum(amount) Is Null, 0, Sum(amount)) AS TotalAmount
FROM Payments

This should work even from C# because Is Null and IIf are both built in to Jet/ACE.这甚至可以从 C# 工作,因为Is NullIIf都内置于 Jet/ACE。 Please note the space in Is Null and the lack of parentheses (it is a statement , not a function ).请注意Is Null的空格和缺少括号(它是一个语句,而不是一个函数)。

There are two added bonuses to using IIf and Is Null as opposed to Nz even if Nz is available to you:Nz相比,使用IIfIs Null有两个额外的IIf ,即使您可以使用Nz

  • it executes faster because all the processing is done within the database engine (so it doesn't have to make function calls to the Access library)它执行得更快,因为所有处理都在数据库引擎中完成(因此它不必对 Access 库进行函数调用)
  • it retains the field's original type;它保留了字段的原始类型; because Nz returns a Variant, Jet/ACE is forced to display the result as a string (which is usually not what you want when dealing with dates, numerics, etc)因为Nz返回一个 Variant,Jet/ACE 被迫将结果显示为字符串(这在处理日期、数字等时通常不是你想要的)

UPDATE : Allen Browne has an excellent primer on the use of IIf , Nz , IsNull() , and Is Null .更新: Allen Browne 有一本关于IIfNzIsNull()Is Null使用的优秀入门书 I was planning on posting that link as my original answer, but I couldn't find the page at the time.我打算将该链接作为我的原始答案发布,但当时我找不到该页面。 I did the best I could from memory, but the true credit goes to Mr. Browne.我从记忆中尽力了,但真正的功劳归功于布朗先生。

Pretty much the equivalent in Access is the nz function. Access 中的等价物几乎是nz函数。

There's a good page on how to use it here .有一个关于如何使用它的一个很好的页面在这里

However, if you're using Access just as a database backend and using Jet in your connectionstring then nz won't be available to you.但是,如果您将 Access 仅用作数据库后端并在连接字符串中使用 Jet,则 nz 将不可用。

同样,也可以在排序时用于日期和时间

.....ORDER BY TRANSDATE ASC,(IIf([PaymentTime] Is Null, '23:59:59', [PaymentTime])) DESC

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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