Syntax: NULLIF ( expression , expression )
Returns a null value if the two specified expressions are equal. NULLIF returns the first expression if the two expressions are not equal. If the expressions are equal, NULLIF returns a null value of the type of the first expression. NULLIF is equivalent to a searched CASE function in which the two expressions are equal and the resulting expression is NULL.
Following is a good example of NULLIF and CASE from BOL:
USE AdventureWorks; GO SELECT ProductID, MakeFlag, FinishedGoodsFlag, NULLIF(MakeFlag,FinishedGoodsFlag)AS 'Null if Equal' FROM Production.Product WHERE ProductID < 10; GO SELECT ProductID, MakeFlag, FinishedGoodsFlag,'Null if Equal' = CASE WHEN MakeFlag = FinishedGoodsFlag THEN NULL ELSE MakeFlag END FROM Production.Product WHERE ProductID < 10; GO
Explanation of ISNULL
ISNULL ( check_expression , replacement_value )
Replaces NULL with the specified replacement value. The value of check_expression is returned if it is not NULL; otherwise, replacement_value is returned after it is implicitly converted to the type of check_expression, if the types are different.
Following is a good example of ISNULL from BOL:
USE AdventureWorks; GO SELECT AVG(ISNULL(Weight, 50)) FROM Production.Product; GO
An interesting observation is NULLIF returns null if it comparison is successful, whereas it returns not null if its comparison is successful. In one way they are opposite to each other.
How to create infinite loop using NULLIF and ISNULL? If this is even possible?
Sidenote: In the SQL Server world, I have seen lots of people getting confused with NULL. They think null is nothing, or null is undefined or is null infinite. Actually, none of the answer is true. Null has been just Null. You can’t compare the null with any other value. This is the reason when we compare null with any other value we get the answer as null. Additionally, when we compare null to null, we get false results because there is no way to identify NULL with any value.
Reference: Pinal Dave (https://blog.sqlauthority.com)