Skip to main content
SQL โ€ข LESSON 121

NULLIF

How can we convert empty strings into true SQL NULLs?

Advancedโฑ 3 Minutesโญ 1340 XP
๐Ÿค” THE QUESTION

How can we convert empty strings into true SQL NULLs?

๐Ÿ’ก WHAT IS IT?

NULLIF(a, b) returns NULL if both arguments are equal; otherwise it returns the first argument.

๐ŸŽฏ WHAT IS IT USED FOR?

Cleaning empty string imports (''), avoiding division by zero errors (NULLIF(denom, 0)).

๐Ÿ’ป EXAMPLE
SELECT
name,
NULLIF(status, '') AS cleaned_status
FROM employees;

๐ŸŽฏ Mission Objectives

Practice typing production-grade SQL code for NULLIF.

  • โœ“ NULLIF function
  • โœ“ Empty string conversion
  • โœ“ Data sanitization
  • โœ“ Division-by-zero defense