An expression computes revenue / items and items may be zero. Which construct prevents a division-by-zero error, and how?
Correct answer
A. NULLIF(items, 0), which returns NULL for a zero divisor so the result becomes NULL
18 views
A. NULLIF(items, 0), which returns NULL for a zero divisor so the result becomes NULL
18 views
NULLIF(a, b) returns NULL when a equals b, so NULLIF(items, 0) turns a zero divisor into NULL and the division yields NULL instead of raising a division-by-zero error. COALESCE does the opposite job, returning the first non-null argument and defaulting missing values. Combining them, revenue / NULLIF(COALESCE(items, 0), 0), is the usual defensive idiom.
Asked in: NTS, PPSC IT, Interviews
Not sure why the answer is A? Ask here and get an explanation.