nvl function not returning expected result - sql

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

Related

How to query empty string in postgresql?

I have table, in which a column has empty fields.when i try to run simple query like below it is not
returning the data.
select * from table1 where "colunm1" = ''
But below two queries returns data
1,
select * from table1
where coalesce("colunm1", '') = ''
2,
select * from table1
where "colunm1" is null
Can someone tell me the reason?
TIA
You have describe the behavior of a column that is NULL. NULL is not the same as an empty string.
It fails any equality comparison. However, you can use is null or (less preferentially) coalesce().
The only database that treats an empty string like a NULL value, is Oracle.
Logically, for a DBMS, that would be wrong.
Relational DBMSs are all about :
set theory
Boolean algebra
A NULL value is a value that we don't know. A string literal of '' is a string whose value we know. It is a string with no characters, with a length of 0. We don't know of a NULL as a string how long it is, how many and, if any, which, characters it contains.
So it is only logical that:
the comparison '' = '' will evaluate to TRUE
the comparison NULL = NULL will evaluate to FALSE , as any comparison with a NULL value will evaluate to FALSE.
And functions like COALESCE() or NVL(), IFNULL(), ISNULL() will return the first parameter if it does not contain a NULL value. That is consistent.
Except in Oracle

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 ISNULL is not returning replacement value

ContactPerson type is nvarchar.
The issue is when ContactPerson is empty not returning n/a
Query
Select ISNULL(ContactPerson,'n/a') from [dbo].[Suppliers]
An empty string ('') and not the same thing as a null value. ISNULL() returns the replacement value only for an actual null value.
You can use NULLIF() to evaluate an empty string into a null value:
SELECT ISNULL(NULLIF(ContactPerson, ''), 'n/a') ...
I would suggest to use COALESCE instead of ISNULL, with NULLIF function:
SELECT COALESCE(NULLIF(ContactPerson,''),'n/a')
FROM [dbo].[Suppliers]
You can find the difference of COALESCE and ISNULL f.e. here.

Oracle : IN and OR

I've a scenrio which process many data in Oracle database. In some cases, the variable Sec_email will contain many values and in some cases Sec_email will contain null or ' '.
so can please any one tell me how to write a query for this?
I tried with
(C.SECONDARY_EMAIL IN ('?,?') OR '' = '' )
where C is the Client table.
When I use this i get the count as 0.
You can perform a not null check before the IN comparison like
Sec_email is not null and C.SECONDARY_EMAIL IN (...
One obvious problem is that Oracle (by default) treats empty strings as NULL. So: '' = '' is the same as NULL = NULL, which is never true.
Arrgh.
In any case, you are probably constructing the query, so use is null instead:
(C.SECONDARY_EMAIL IN ('?,?') OR '' IS NULL
I think the real problem, though, is the first comparison. The IN list has one element with a constant, not two (but perhaps that is your intention). If you want to put a variable number of values for comparison, one method uses regular expressions. For instance:
C.SECONDARY_EMAIL REGEXP_LIKE '^val1|val2|val3$' or '' IS NULL
If you would like to get a list of values when some of them is null you should use:
("some other conditions" OR C.SECONDARY_EMAIL IS NULL)
The question is if it is not null and not ' ' value what you are expecting, if it should be some king of
pattern you should use regular expression:
regexp_like(C.SECONDARY_EMAIL, '^(.+?[,]+?)+$')
Also, if you have a few conditions in where clause use should use brackets to group you conditions null check and another one.
All conditions i this case will be divided by OR.
(C.SECONDARY_EMAIL IS NULL OR regexp_like(C.SECONDARY_EMAIL, '^(.+?[,]+?)+$'))
or
(C.SECONDARY_EMAIL IS NULL OR regexp_like(C.SECONDARY_EMAIL, '^(.+?[,]+?)+$')
OR C.SECONDARY_EMAIL = ' ')

What does the translate function do in Oracle

I know the decode function, however, no idea what the stuff in translate means:
decode(translate(cookie_id,'0123456789','') = '','t',cookie_id,null) as cookie_id
decode((translate(pickup_date,'0123456789','') = '--' and length(pickup_date) = 10),'t',pickup_date,null) as pickup_date,
It is very simple. This expression:
translate(cookie_id, '0123456789', '')
returns NULL. Oracle treats the empty string as NULL and even explicitly warns:
You cannot use an empty string for to_string to remove all characters
in from_string from the return value. Oracle Database interprets the
empty string as null, and if this function has a null argument, then
it returns null.
So, the first example will alway yield NULL for the cookie_id.
Quite possibly, the author intended something like:
decode(translate(cookie_id, 'a0123456789', 'a'), '', cookie_id, null) as cookie_id
This would check to see if cookie_id only contains digits and then return the id. Personally, I find a regular expression easier to follow:
(case when regexp_like(cookie_id, '^[0-9]+$') then cookie_id end)
And this doesn't have to do funky things to deal with empty strings.