Oracle SQL_Null Related functions-3_NULLIF

NULLIF

admin Functions, Oracle ,

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(expr1, expr2)

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

Sharing is caring!

You May Also Like..

SUBSTR

Oracle_SUBSTR()

Hey alians, SUBSTR is pretty cool when it comes to extract a substring out of a string in SQL. SUBSTR […]

INSTR

Oracle_INSTR()

Hey alians, While working with the data, INSTR is a very important single row function that returns a numeric value. […]

DUAL TABLE

Oracle_DUAL table

Hey geeks, DUAL table is a very important table available in the Oracle database. It gets created automatically during the […]

Leave a Reply

Your email address will not be published. Required fields are marked *