Skip to main content
SQL • LESSON 121

NULLIF

How can we convert empty strings into true SQL NULLs?

Advanced3 Minutes1340 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