How is the nullif function used in Oracle?
In Oracle, the NULLIF function is used to compare two expressions and return NULL if their values are equal, otherwise it returns the value of the first expression. The syntax of the NULLIF function is as follows:
Returns NULL if expr1 is equal to expr2, otherwise returns expr1.
expr1 and expr2 are the two expressions being compared.
The main purpose of using the NULLIF function is to prevent division by zero errors when comparing two expressions. If the values of expr1 and expr2 are equal, the NULLIF function will return NULL, making it useful for avoiding division by zero errors.
Here is an example of using the NULLIF function:
Return value 10 if not equal to 5, otherwise return NULL.
In the examples above, the first expression is 10 and the second expression is 5. Since these two values are not equal, the NULLIF function returns the value of the first expression, which is 10.
If the first expression in the example is changed to 5, then:
Return NULL if 5 is equal to 5 in the table DUAL.
Since 5 equals 5, the NULLIF function will return NULL.
In summary, the NULLIF function is used to compare the values of two expressions, returning NULL if they are equal or the value of the first expression if they are not. This function is commonly used to prevent division by zero errors.