Convert Null to zero in access database

微笑、不失礼 提交于 2019-12-02 09:17:54

问题


Considering that Grade.firstExam, Grade.secondExam, and Grade.finalExam are all TEXT and not numbers, i can't get the exact solution to convert null values into Zeros. I already tried to use NZ() but it returns an error message (Undefined Function 'NZ' in expression) im using VB.net with MS Access as my database. i also imported System.Data.OleDb Namespace.

    SQLStatement = " SELECT     Student.idNumber as 'IDno', "
    SQLStatement &= "           Student.lastName + ', ' + Student.firstName as 'FullName', "
    SQLStatement &= "           Grade.firstExam as 'firstExam', "
    SQLStatement &= "           Grade.secondExam as 'secondExam', "
    SQLStatement &= "           Grade.finalExam as 'finalExam', "
    SQLStatement &= "           ((CDBL(Grade.firstExam) + CDBL(Grade.secondExam) + CDBL(Grade.finalExam) + CDBL(Grade.finalExam)) / 4) as 'Average' "
    sqlstatement &= "FROM       Student LEFT OUTER JOIN Grade ON "
    sqlstatement &= "           Student.idNumber = Grade.idNumber "
    sqlstatement &= "WHERE      Student.idNumber = '" & StudentID & "' "
    SQLStatement &= "ORDER BY   ((CDBL(Grade.firstExam) + CDBL(Grade.secondExam) + CDBL(Grade.finalExam) + CDBL(Grade.finalExam)) / 4) DESC"

How can i convert empty string or null that exist in the field?


回答1:


In your SQL try something like this:

SQLStatement &= "          IIf(IsNull(Grade.firstExam),'0',Grade.firstExam) as 'firstExam', "



回答2:


You are getting this message error probably because you are using the VB function from inside the SQL request:

mySQLRequest = "SELECT ..., NZ(yourNumber,0), ..."

the NZ function is not understood by the database engine

You should write down your request this way:

mySQLRequest = "SELECT ..., IIf([myField] Is Null,0,[myField]) as myField, ..."


来源:https://stackoverflow.com/questions/4705738/convert-null-to-zero-in-access-database

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