Saturday, November 10, 2012

How to Handle Null Values in Oracle

My classic asp program was like this:

Set ObjRs=Myconn.Execute("select max(record_id) as last_record_id from student_master").

I am supposed to add one to this value for creating a new record_id.
last_record_id=cint(objrs("last_record_id"))


But when there was no records in that table, it created an error like this:

Error Type:
Microsoft VBScript runtime (0x800A005E)
Invalid use of Null: 'cint'

So, What is the solution?

Whenever there is null value in a field,  oracle should not rerurn NULL, Instead it should return zero.

For this, modify above program like this:



Set ObjRs=Myconn.Execute("select nvl(max(record_id),0) as last_record_id from student_master").

I am supposed to add one to this value for creating a new record_id.
last_record_id=cint(objrs("last_record_id"))

The nvl function returns ZERO whenever it gets a NULL value.

It is a very usefull function. Remember it!

NVL(FIELDNAME,0)