đ The NULLIF() function compares two expressions and returns NULL if they are equal. If the two expressions are different, it returns the value of the first expression. NULLIF() is commonly used to avoid divide-by-zero errors, normalize data, and simplify conditional logic.
đ What is NULLIF()?
NULLIF() evaluates two expressions. If both expressions have the same value, the result is NULL. Otherwise, the first expression is returned unchanged.
Information
đ¯ Why Use NULLIF()?
NULLIF() is useful when a specific value should be treated as missing or when preventing runtime errors in calculations.
- đ Prevent divide-by-zero errors.
- đ Convert specific values into NULL.
- đ Simplify conditional expressions.
- đ Normalize imported or inconsistent data.
- đ Improve query readability.
đ Sample Table
| ProductID | ProductName | TotalSales | TotalOrders |
|---|---|---|---|
| 101 | Laptop | 50000 | 25 |
| 102 | Keyboard | 10000 | 0 |
| 103 | Mouse | 15000 | 30 |
đ Basic Syntax
NULLIF() Syntax
NULLIF(expression1, expression2)If expression1 equals expression2, the function returns NULL. Otherwise, it returns expression1.
đĄ Example: Basic Comparison
Simple NULLIF() Example
SELECT NULLIF(10, 10) AS Result;Since both values are equal, the result is NULL.
Different Values
SELECT NULLIF(10, 20) AS Result;Since the values are different, the result is 10.
đĄ Example: Prevent Divide-by-Zero
One of the most common uses of NULLIF() is avoiding division by zero.
Safe Division
SELECT
ProductName,
TotalSales / NULLIF(TotalOrders, 0) AS AverageSale
FROM Products;If TotalOrders is 0, NULLIF() returns NULL, preventing a divide-by-zero error. The result of the division is typically NULL instead of causing the query to fail.
đĄ Example: Convert Empty Values to NULL
NULLIF() can convert a specific value into NULL.
Convert Empty String to NULL
SELECT
EmployeeName,
NULLIF(PhoneNumber, '') AS PhoneNumber
FROM Employees;Empty strings become NULL, while valid phone numbers remain unchanged.
đ Example Output
| ProductName | AverageSale |
|---|---|
| Laptop | 2000 |
| Keyboard | NULL |
| Mouse | 500 |
âī¸ NULLIF() vs COALESCE()
| Feature | NULLIF() | COALESCE() |
|---|---|---|
| Purpose | Returns NULL if two values are equal. | Returns the first non-NULL value. |
| Arguments | Exactly 2 | 2 or more |
| Typical Use | Conditional NULL conversion. | Replace NULL values. |
| SQL Standard | â Yes | â Yes |
âī¸ NULLIF() vs CASE
NULLIF() is functionally equivalent to a simple CASE expression.
Equivalent CASE Expression
CASE
WHEN expression1 = expression2 THEN NULL
ELSE expression1
ENDNULLIF() offers a shorter and more readable way to express this logic.
đŧ Real-World Examples
- đ° Prevent divide-by-zero errors in financial reports.
- đ Convert placeholder values such as 0 or empty strings into NULL.
- đĻ Normalize imported datasets.
- đ Improve statistical calculations that should ignore certain values.
- đ Simplify SQL expressions compared to using a full CASE statement.
đī¸ Database Compatibility
| Database System | NULLIF() Support |
|---|---|
| MySQL | â Fully supported. |
| PostgreSQL | â Fully supported. |
| SQL Server | â Fully supported. |
| Oracle | â Fully supported. |
| SQLite | â Fully supported. |
â ī¸ Common Mistakes
- â Expecting NULLIF() to replace every NULL value automatically.
- â Passing incompatible data types as arguments.
- â Forgetting that the function returns the first expression when values differ.
- â Using NULLIF() where COALESCE() or CASE is more appropriate.
Warning
â ī¸ Best Practices
Best Practice
đ Key Points to Remember
- đ NULLIF() compares two expressions.
- đ It returns NULL when both expressions are equal.
- đ Otherwise, it returns the first expression.
- đ It is commonly used to prevent divide-by-zero errors.
- đ It is part of the ANSI SQL standard.
- đ It is supported by most major relational database systems.