Concept Flow - NULLIF function behavior
Start NULLIF(a, b)
Compare a and b
Return NULL
End
NULLIF compares two values; if they are equal, it returns NULL, otherwise it returns the first value.
SELECT NULLIF(5, 5) AS result1, NULLIF(5, 3) AS result2;
| Step | Input a | Input b | Condition (a = b)? | Output |
|---|---|---|---|---|
| 1 | 5 | 5 | True | NULL |
| 2 | 5 | 3 | False | 5 |
| 3 | NULL | NULL | UNKNOWN | NULL |
| 4 | NULL | 5 | UNKNOWN | NULL |
| Variable | Start | After Step 1 | After Step 2 | After Step 3 | After Step 4 |
|---|---|---|---|---|---|
| a | 5 | 5 | 5 | NULL | NULL |
| b | 5 | 5 | 3 | NULL | 5 |
| Output | N/A | NULL | 5 | NULL | NULL |
NULLIF(a, b) returns NULL if a equals b. Otherwise, it returns a. If both inputs are NULL, it returns NULL. Useful to avoid division by zero or replace equal values with NULL.