Is there a built in function in sql2000 that returns 0 or 0.00 if money or a float column is null? - sql

Is there a simple built in function in sql2000 that returns 0 or 0.00 if money or a float column is null? Or do I need to build the function myself, and how would I build it? if (isnull(col1)) then 0 else col1?

Yes, the function is called ISNULL, you use it like this:
SELECT ISNULL(columnname, 0.0) AS xyz FROM tablename
You basically specify which value to return in case the first value is null.
Note that ISNULL does not return a boolean value indicating whether the parameter is null or not, instead you pass it 2 parameters and it does the following:
If the first argument is non-null, return the value of the first argument
Otherwise, return the second argument
You can also use the COALESCE function for this, which takes an unspecified number of parameters, and returns the first non-null argument value back, in the order they're specified.
In other words:
ISNULL(columnname, 0.0)
and
COALESCE(columnname, 0.0)
behaves the same, but I don't know if there is a difference in "sargeability" of these two, ie. whether they can use indexes or not.

Related

Division by NULL in IBM DB2

In Oracle, any number divided by NULL returns NULL. I was wondering what is the case for DB2 Databases?
The whole point of it is to check whether the following expression behaves in the same way for Oracle and DB2:
SELECT a / NULLIF(b, 0) FROM some_table;
Say b=0, we would get a division by null.
The NULLIF function returns the null value if the two arguments are equal; otherwise, it returns the value of the first argument.
-NULLIF(expression,expression)-------------------------------
The result of using NULLIF(e1,e2) is the same as using the CASE expression:
CASE WHEN e1=e2 THEN NULL ELSE e1 END
Copy
When e1=e2 evaluates to unknown because one or both arguments is null, CASE expressions consider the evaluation not true. In this case, NULLIF returns the value
of the first argument.
IBM DB2 docs
So for DB2 and oracle it works same way
Db2 returns null when one of the operators in a math expression is null. For example
values (3 / cast(null as integer)) will return null.
On the other hand, the definition of NULLID is equal to that in Oracle. If both arguments are equal, it will return null.

Select with NVL2 Not working Oracle

I am selecting values from the database using Functions. I have three different functions in which if the first does not return anything I check for the second and third functions for values using NVL2. The problem is when I execute independent functions they return values but not in the NVL2 function My query is:
select NVL2(CRBP.FPDATE(LAM.ACID),
CRBP.FPDATE1(LAM.ACID),
CRBP.ODSANCDATE(GAM.ACID)) from
tbaadm.lam, TBAADM.GAM
where gam.acid = lam.acid
and gam.acid = 'VM12990'
This does not return any value.
But when I execute :
select CRBP.FPDATE (LAM.ACID) from tbaadm.lam where lam.acid = 'VM12990';
This Already returns a value. Shouldn't this be the value returned by my first query considering that it is the first in the check. What is the Problem with My first Query??
If CRBP.FPDATE returns a value the query should return the value returned by the function CRBP.FPDATE1 and if CRBP.FPDATE returns NULL then the query should return CRBP.ODSANCDATE's vlaue.
Does CRBP.FPDATE1 return anything?
NVL2(a,b,c) is a "if a is null then a else b" type function. so the value of a is never returned.
it sounds like you want COALESCE which will return the first not null value from the set of parameters

Set a maximum value for an expression

I am trying to create a score based on the char_length of a field. I am using a fraction of the char_length returned.
(CASE WHEN (char_length(e.summary)/100) is null THEN +0
ELSE +(char_length(e.summary)/100) END)
I would like to know how to set a maximum return value. Is there a simple function or do I need to do one more CASE WHEN with if >=10 THEN ... ?
Simplify to:
LEAST(COALESCE(length(e.summary)/100, 0), 10)
Use LEAST() to introduce an upper border as #Mark already mentioned.
Use COALESCE() to provide a default for NULL values.
length() does the same as char_length()
You can use the least function.
least(<YOUR CALC HERE>, 10) limits the maximum value returned to 10.

sql question what is this statement meaning

What does this statement meaning, could somebody elaborate me on this please
(#diplomaPercentage is null OR diploma_percentage>=#diplomaPercentage)
Will be returned all rows if diplomaPercentage is not specified (i.e. passed null), in other cases will be returned rows where diploma_percentage more or equal #diplomaPercentage. :)
This kind of condition is usually used to avoid "dynamic SQL", but it makes the code ugly and ironically may result in worse performance compared to just using dynamic SQL. You can read more about this at:
http://www.sommarskog.se/dyn-search-2005.html
#diplomaPercentage is a variable.
#diplomaPercentage is null is checking if the variable is NULL or not and
diploma_percentage>=#diplomaPercentage is checking if the column value diploma_percentage is greater than or equal to variable value
If you give it a null value for #diplomapercentage then it will return all records, otherwise it will it return only the records with a diploma_percentage value greater than or equal to the one you supply.
If no value for #diplomaPercentage is passed it it will return all rows, otherwise it will return all the rows where diploma_percentage is greater than #diplomaPercentage if a value has been passed in

SQLite equivalent of PostgreSQL's GREATEST function

PostgreSQL has a useful function called GREATEST. It returns the largest value of those passed to it as documented here.
Is there any equivalent in SQLite?
As a note, I only need it to work with 2 arguments.
SELECT MAX(1,2,..)
ref: https://sqlite.org/lang_corefunc.html#maxoreunc
max(X,Y,...)
The multi-argument max() function returns the argument with the maximum value, or return NULL if any argument is NULL. The multi-argument max() function searches its arguments from left to right for an argument that defines a collating function and uses that collating function for all string comparisons. If none of the arguments to max() define a collating function, then the BINARY collating function is used. Note that max() is a simple function when it has 2 or more arguments but operates as an aggregate function if given only a single argument.
using a second value in MAX(value1, value2) would be the equivalent
Example:
UPDATE products SET Quantity = MAX(Quantity - #value, 0)...
if (Quantity - value) return a "Negative Number -0" then Max( , 0) will return 0 because 0 is bigger than -0 / -1 / -2 ... and so on
Max( , 1) will return 1 if the same condition (Quantity - value) return 0 or a Negative Number .. etc, you get the idea !
if we assume that both Quantity and #value could be NULL then use the combination: IFNULL(MAX(Quantity-#value,0),0)
IFNULL(..., 0) will return the second value of your choice IF the first one is NULL