Division by NULL in IBM DB2 - sql

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.

Related

nvl function not returning expected result

I know that nvl function works only for 'null', not empty string. I just wanted to know if there was a function that helps me replace blank/null values with another one.
I used instead:
CASE WHEN expression_1 is null or expression_1 = '' then expression_2 else expression_1
expression_1 column has empty cells, not null cells.
NVL() is a function most typically associated with Oracle. The equivalent standard SQL function is COALESCE().
By default, Oracle treats NULL strings and empty strings ('') as the same. So, '' IS NULL evaluates to "true" (and '' = '' rather confusingly evaluates to NULL, which is false in a WHERE clause).
So, you can use NVL() or COALESCE() on an empty string. These are the same:
NVL(NULL, 'A')
NVL('', 'A')
COALESCE(NULL, 'A')
COALESCE('', 'A')
Here is a db<>fiddle.
Incase the column contains blank spaces, you can use TRIM() besides NVL() so as you can gracefully remove blank spaces before you apply NVL().
NVL(TRIM(' '),'X') --> returns X
db<>fiddle -
https://dbfiddle.uk/?rdbms=oracle_18&fiddle=08435972cbfb799329e6609349c70a04
I believe you are looking Coalesce ():
coalesce(expression2, expression1)
Brings the first non-null value from expression2 else expression 1. If all expressions evaluate to null, then the COALESCE function will return null.
NVL function lets you substitute a value when a null value is encountered:
nvl(expression2, expression1)
Sql statement would replace expression 2 with with expression 1 if it finds null value in there.
Difference: NVL always evaluates both arguments, while COALESCE usually stops evaluation whenever it finds the first non-NULL
One awesome explanation given by #Quassnoi here

Hive - how to check if a numeric columns have number/decimal?

I am trying to generate a hive query which will take multiple numeric column names and check whether it is has numeric values. If the column has numeric values then the output should be (column name,true) else if the field has NULL or some string value the output should be (column name,false)
SELECT distinct (test_nr1,test_nr2) FROM test.abc WHERE (test_nr1,test_nr2) not like '%[^0-9]%';
SELECT distinct test_nr1,test_nr2 from test.abc limit 2;
test_nr1 test_nr2
NULL 81432269
NULL 88868060
the desired output should be :
test_nr1 false
test_nr2 true
Since test_nr1 is a decimal field and it has NULL values, it should output false.
Appreciate valuable suggestions.
You can use cast function. It returns NULL when the value can not not be cast to numeric.
For example:
select case when cast('23ccc' as double) is null then false else true end as IsNumber;
You're trying to use character class pattern matching syntax here, and it doesn't work in every SQL implementation IIRC, however, regexp matching works in most, if not all, SQL implementations.
Considering you're using hive, this should do it:
SELECT ('test_nr1', test_nr1 RLIKE '\d'), ('test_nr2', test_nr2 RLIKE '\d') FROM test.abc;
You should remember that regexp matching is very slow in SQL though.

Oracle DECODE not working

I have an oracle decode that is checking if a value is NULL before updating the decimal precision. The problem is when the value in the price_precision column isn't null the decode still goes to the d.price value, but it should go to the default value. Here is the line of code for the decode:
DECODE(d.PRICE_PRECISION, NULL, d.price,TO_CHAR(DECODE(d.price,NULL, '', d.price), CONCAT('9999990',RPAD('D', d.PRICE_PRECISION+1,'9')))) price
I know for a fact there is non-NULL data in the Price _Precision column, because I can see it in the return for the select statement. Is there something wrong with my decode? any ideas why the decode isn't going to the default statement?
It seems implicit conversion took place. Consider this
DECODE(d.PRICE_PRECISION, NULL, to_char(d.price), TO_CHAR....
From Oracle docs:
Oracle automatically converts expr and each search value to the
datatype of the first search value before comparing. Oracle
automatically converts the return value to the same datatype as the
first result.
For Null values, use NVL function.
select nvl(name,'not registered') from table;
When name is null values, return 'not registered'.
You can use this together with DECODE function.
decode(nvl(PRICE,'Not valid'),'Not valid',0,PRICE)
In calculations, this avoids problems.
From Oracle docs:

Are arithmetic equations implicitly evaluated in NULLIF()

My SQL Fundamentals 1 Exam Guide states,
select NULLIF(1234, 123+1) from dual;
The arithmetic equation is not implicitly evaluated...
However, when I submit the query below the result is null - it seems that 123+1 is evaluated (although I know that the queries are different).
select NULLIF(124, 123+1) from dual;
So, which is correct (for the 1Z0-051 Exam, anyway)? Are expressions evaluated or not?
Nullif() function evaluates both arguments and returns NULL if argument1 equal to argument2. In your case 123+1 is not equal to 1234. 123+1 is equal to 124 not 1234.
the result is null
It cannot be null in your case.
SQL> select nullif(1234, 123+1) as res
2 from dual;
RES
----------
1234
SQL> select nullif(1234, 1233+1) as res
2 from dual
3 ;
RES
----------
NULL
FOLLOW UP: My 2 cents
The above NULLIF is equivalent to the below case
CASE WHEN 1234=123+1 THEN NULL ELSE 1234 END
You cannot specify the literal NULL for the first expression.
Also from documentation
If both arguments are numeric datatypes, then Oracle Database
determines the argument with the higher numeric precedence, implicitly
converts the other argument to that datatype, and returns that
datatype. If the arguments are not numeric, then they must be of the
same datatype, or Oracle returns an error.

SQL Server : Computed Column DATEDIFF Clause

What I'm trying to do is the following: I have 3 columns (Control_OpenDate, Control_Record Age, Control_Stage2). Once the row is inserted it will populate Control_OpenDate (01/27/2013) and Control_RecordAge (computed column, see formula).
(datediff(day, [Control_OpenDate], getdate()))
which gives the days up to date.
Everything is working perfect but I want to add like an IF condition to the computed column whenever the column Control_Stage2 is populated if not, do not calculate or add a text...
How can I add the WHERE-statement in the above formula??
Note: I'm entering such formula directly into the column properties, I know there are queries that can do this but is there a way to do it trough a formula.
This can be done using a CASE-statement, as shown here.
Your logic will then look like:
(CASE
WHEN [Control_Stage2] IS NULL THEN NULL -- or -1 or what you like
ELSE datediff(day,[Control_OpenDate],getdate())
END)
This can also be written as a ternary statement (IIF)
IIF ( boolean_expression, true_value, false_value )
IIF is a shorthand way for writing a CASE expression. It evaluates the Boolean expression passed as the first argument, and then returns either of the other two arguments based on the result of the evaluation. That is, the true_value is returned if the Boolean expression is true, and the false_value is returned if the Boolean expression is false or unknown
E.G.
iif([Control_Stage2] is null, null, datediff(day,[Control_OpenDate],getdate()))