NULLIF Function

The NULLIF function compares two expressions and returns NULL if they are equal; otherwise, it returns the first expression.

Example: Using NULLIF

SELECT Name, NULLIF(Contribution, 'None') AS ValidContribution
FROM FreedomFighters;

Output:

Returns NULL if the contribution is 'None'; otherwise, returns the contribution.

Do's and Don'ts

Do's

  • Use NULLIF to handle specific cases where NULL needs to be returned.
  • Check for potential NULL values in your application logic.

Don'ts

  • Don't use NULLIF when simple conditional logic can be more clear.
  • Don't assume NULL values are always desirable; handle them appropriately.