Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

A+B when B is null?

Status
Not open for further replies.

mike777

Programmer
Jun 3, 2000
387
US
Hello all.
Is there any way in Interbase to add two numbers when one of the numbers is null?
In MS Access, I would say:
Code:
A+nz(B,0)
.
Any suggestions?
Many thanks.
-Mike
 
Let me clarify. I'm trying to do this in Select statement, so in MS Access, I would say:
Code:
select A+nz(B,0) from my table
.
Thanks.
 
You could use/create a UDF for that purpose. FreeUDFLibC has a ROUND UDF (let's call it F_ROUND) that could do the job if you want NULLs to be treated as 0.

Code:
SELECT a+F_ROUND(b) FROM tablename


If you don't want/can't use UDFs, you could always use a small stored proc and use a more complicated query:

Code:
SELECT t.a+(SELECT result FROM nz(t.b, 0)) FROM tablename t
or even:
Code:
SELECT (SELECT result FROM nz(t.a+t.b, t.a)) FROM tablename t

WHERE nz is something like:
Code:
CREATE PROCEDURE NZ (
    VAL INTEGER,
    DEFVAL INTEGER)
RETURNS (
    RESULT INTEGER)
AS
BEGIN
  IF (val IS NULL) THEN
    result = defVal;
  ELSE
    result = val;
  SUSPEND;
END
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top