what is the alternative to NVL function in MS Access 2007

僤鯓⒐⒋嵵緔 提交于 2021-01-26 04:51:35

问题


I wrote an SQL query in MS Access

select NVL(count(re.rule_status),0) from validation_result re, validation_rules ru where re.cycle_nbr="+cycle_nbr+" and re.rule_response=ru.rule_desc and re.rule_status='FAIL' and ru.rule_category='NAMING_CONVENTION' group by re.rule_status"

But the output is Null. I want to convert it to Zero. If I use NVL function then MS Access does not accept it. I tried NZ function also but that also gives the same output, i.e NULL instead of Zero.


回答1:


Nz() is definitely the function you're looking for. You say that you tried it and it returned Null, but I find that hard to believe because the whole point of Nz() is to not return Null. For reference:

x = Nz(Null, 0) returns 0 (VbVarType.vbInteger)

x = Nz(Null, "") returns an empty string (VbVarType.vbString)

x = Nz(Null) returns an empty variable (VbVarType.vbEmpty, not VbVarType.vbNull)

Edit

Further investigation shows that the problem in your particular case is that you are doing a COUNT(re.rule_status) in a query that also does GROUP BY re.rule_status. If the WHERE clause of the query results in an empty set (no rows returned) then the overall query simply returns no rows instead of a single row with a value of 0 or Null.

This can be verified with the following test code...

Sub NzTest()
Dim rst As DAO.Recordset, strSQL As String
strSQL = "SELECT Nz(COUNT(LastName), 0) FROM Members WHERE False GROUP BY LastName"
Debug.Print strSQL
Set rst = CurrentDb.OpenRecordset(strSQL, dbOpenSnapshot)
If rst.EOF Then
    Debug.Print "No rows were returned."
Else
    Debug.Print "Count = " & rst(0).Value
End If
rst.Close
Set rst = Nothing
End Sub

...which produces the result

SELECT Nz(COUNT(LastName), 0) FROM Members WHERE False GROUP BY LastName
No rows were returned.

When the GROUP BY is removed we get...

SELECT Nz(COUNT(LastName), 0) FROM Members WHERE False
Count = 0

...and in fact Nz() is not even required in that case:

SELECT COUNT(LastName) FROM Members WHERE False
Count = 0


来源:https://stackoverflow.com/questions/16828310/what-is-the-alternative-to-nvl-function-in-ms-access-2007

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!