Hi Aliens,
Today, we will talk about another Null Related Function, NULLIF.
You can read my notes on NVL, NVL2 and COALESCE. Please have a look if you haven’t yet.
Syntax:
NULLIF:
As you can see in the above syntax that NULLIF accepts only 2 parameters. If expr1 and expr2 are same then function returns NULL else expr1.
Parameters can be column name as well as any literal value. Also, expr1 can never be literal NULL and both parameters should be in same datatype.
Let’s do some examples as they always play a major part in understanding the concept.
Example-1
SELECT
nullif(100,100) AS nullif
FROM
dual;
Output…
NULLIF
----------
(null)
You can see in the above code. expr1 and expr2 are same so function returned NULL.
Example-2
SELECT
nullif(100,200) AS nullif
FROM
dual;
Output…
NULLIF
----------
100
This time expr1 and expr2 are not same so function returned expr1.
What if we put expr1 in number datatype and expr2 in char datatype.
Example-3
SELECT
nullif(100,'100') AS nullif
FROM
dual;
Output…
ORA-00932: inconsistent datatypes: expected NUMBER got CHAR
00932. 00000 - "inconsistent datatypes: expected %s got %s"
*Cause:
*Action:
Error at Line: 17 Column: 13
So in this case, it returns an error. So expr1 and expr2 should always be in same datatype.
That’s it for NULLIF. If you have any questions/suggestions, please comment below or write me an email.
If you like this blog, do share with your friends and colleagues on your social media.
For more updates join my facebook group and do like my facebook page.
Thank you,
Kapil Kumar
dbgeek.in