My nullif doesnt seem to be working when v2 is null, do I have this right?
SELECT ROUND (v1 / NULLIF (v2, 0), 3) FROM t1 WHERE id = 100
for eg: when V1=58 and V2 is null, the result should be 58 but its returning null.
You are confusing NULLIF()
and COALESCE()
. Your expression is correct for preventing divide-by-zero. It is exactly the right thing to use in this situation, and it is working correctly.
If you want to treat a NULL
value as 1
, you could do something like this:
SELECT ROUND(v1 / (CASE WHEN v2 IS NULL THEN 1
WHEN v2 <> 0 THEN v2
END), 3)
FROM t1
WHERE id = 100;
That seems like odd logic to me.
Please try this:
SELECT ROUND (v1/ COALESCE(v2,1), 3)
FROM FROM t1
WHERE id = 100;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.