473,395 Members | 1,677 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

add calcuation with NULL record

i use the below formula to add up records which is extracted from SQL by
ASP.
sumQS = cint(objRS1("Q1S")) + cint(objRS1("Q2S")) + ....

but if cint(objRS1("Q1S")) is null, it gets error.

i read solution from book that add ZERO value to those NULL variables by
using IsNULL.

is it the only way to deal with this cos i have lots of these variables?
then lots of code.

Thanks a lot.

tony
Feb 14 '06 #1
4 2792
Tony WONG wrote:
i use the below formula to add up records which is extracted from SQL
by ASP.
sumQS = cint(objRS1("Q1S")) + cint(objRS1("Q2S")) + ....
This seems like bad database design to me. If all of your QS values were
stored in a single column in a database table, a simple grouping query would
vastly simplify this task you have set yourself.

but if cint(objRS1("Q1S")) is null, it gets error.

i read solution from book that add ZERO value to those NULL variables
by using IsNULL.

is it the only way to deal with this cos i have lots of these
variables? then lots of code.


One thing you can do is use functions in your sql statement that generates
this recordset to guarantee that no nulls are returned. I can't get more
specific due to your failure to tell us what database you are using, but I'm
sure if you looked at the online help for the SQL variant used by your
database, you can find those functions (COALESCE for SQL Server, Iif for
Access)

That said, it is always costly to directly read values from your recordset
object. If you are using the same values more than once, especially inside a
loop, you can greatly improve performance by assigning the values to
variables. Assigning the values to variables is the perfect time to make
sure that nulls are handled. You can put this function in your script
library:

Function HandleNull(data)
if data is null then HandleNull = 0
End Function

Then, when processing your recordset:

dim q1s, q2s, ...
q1s=HandleNull(objRS1("q1s").value)
etc.

You should consider that other datatypes (string, date, etc) can also
contain nulls, so you will want to handle those differently.

Bob Barrows
--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"
Feb 14 '06 #2
Bob

Thanks a lot for your detail analysis and advice.
"Bob Barrows [MVP]" <re******@NOyahoo.SPAMcom> ¼¶¼g©ó¶l¥ó·s»D:O7*************@TK2MSFTNGP10.phx.gb l...
Tony WONG wrote:
i use the below formula to add up records which is extracted from SQL
by ASP.
sumQS = cint(objRS1("Q1S")) + cint(objRS1("Q2S")) + ....


This seems like bad database design to me. If all of your QS values were
stored in a single column in a database table, a simple grouping query
would vastly simplify this task you have set yourself.

but if cint(objRS1("Q1S")) is null, it gets error.

i read solution from book that add ZERO value to those NULL variables
by using IsNULL.

is it the only way to deal with this cos i have lots of these
variables? then lots of code.


One thing you can do is use functions in your sql statement that generates
this recordset to guarantee that no nulls are returned. I can't get more
specific due to your failure to tell us what database you are using, but
I'm sure if you looked at the online help for the SQL variant used by your
database, you can find those functions (COALESCE for SQL Server, Iif for
Access)

That said, it is always costly to directly read values from your recordset
object. If you are using the same values more than once, especially inside
a loop, you can greatly improve performance by assigning the values to
variables. Assigning the values to variables is the perfect time to make
sure that nulls are handled. You can put this function in your script
library:

Function HandleNull(data)
if data is null then HandleNull = 0
End Function

Then, when processing your recordset:

dim q1s, q2s, ...
q1s=HandleNull(objRS1("q1s").value)
etc.

You should consider that other datatypes (string, date, etc) can also
contain nulls, so you will want to handle those differently.

Bob Barrows
--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"

Feb 15 '06 #3
Tony WONG wrote:
Function HandleNull(data)
if data is null then HandleNull = 0
End Function


Wow! What a stupid mistake! I can't believe nobody called me on this. The
function, of course, should read:

Function HandleNull(data)
if data is null then
HandleNull = 0
else
HandleNull = data
end if
End Function
--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"
Feb 15 '06 #4
never mind

i already fixed your formula into my case.

your formula have already helped me a lot.

thanks.

"Bob Barrows [MVP]" <re******@NOyahoo.SPAMcom> ¼¶¼g©ó¶l¥ó·s»D:%2***************@tk2msftngp13.phx. gbl...
Tony WONG wrote:
Function HandleNull(data)
if data is null then HandleNull = 0
End Function


Wow! What a stupid mistake! I can't believe nobody called me on this. The
function, of course, should read:

Function HandleNull(data)
if data is null then
HandleNull = 0
else
HandleNull = data
end if
End Function
--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"

Feb 20 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: allyn44 | last post by:
Hello, I have a table that has null fields that need to be filled in with the value of the previous record (example below) id date 1 2/2/02 2 3 4/4/02 4
5
by: jannordgreen | last post by:
The form 'Client Records' has this record source: SELECT ., .LastName, .FirstName, .Company, .Address, .City, ., .State, .Country, .WorkPhone, .FaxNumber, .EmailAddress, .Business, .Origin, .,...
1
by: Daniel Martini | last post by:
Hi all, I'm currently coding some functions in plpgsql for generating reports out of records in a table. Problem is: NULL values in records make the complete function fail. Here is a simple...
3
by: Stinky Pete | last post by:
Hi, I work in a manufacturing environment that needs to record non conformances (NCF) against goods or processes. At the moment, QA can open a form to automatically generate a new NCF number...
2
by: Brett | last post by:
My database has 2 tables: Table1 & Table2. If a field is not null on a record in table2, then the not null fields in table1 that correspond to the records in table1 needs to be updated to match the...
13
by: Paul Lautman | last post by:
I have the following switch statement: switch ($record->sub_page) { case -1: $this->page = 6; $error_message = '<center>You appear to have already completed the form.</center>'; break; case...
0
by: hardieca | last post by:
Hi, My formview or gridview control stops updating or deleting a record once the record has a null value. I have table tblTest with the following pkID int NOT NULL **IDENTITY COLUMN**...
4
by: Debbiedo | last post by:
I searched the groups and tried several approaches but still cannot find a solution. I have a table that has several hundred fields that may or may not need to be displayed in a report,...
2
by: BobLewiston | last post by:
Some of you may have seen my earlier thread “PasswordHash NULL problem”. I’ve started a new thread because investigation has shown that the problem is actually quite different than I previously...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.