TRY_DIVIDE()
All functions > MATH > TRY_DIVIDE()
Same as DIVIDE, but returns NULL if the divisor is zero.
Signatures
Safe numeric division
Returns: Result of the division, or NULL if the divisor is zero
TRY_DIVIDE(dividend: T, divisor: U) → DOUBLE sql
| Parameter | Type | Required | Description |
|---|---|---|---|
dividend | T | Yes | Number to be divided |
divisor | U | Yes | Number to divide by |
With:
T: Any numeric typeU: Any numeric type
Signature notes:
- Both arguments are cast to DOUBLE before division
- Returns NULL if either operand is NULL or if the divisor is zero
- Result is always a DOUBLE, even for integer inputs
- Safe alternative to DIVIDE when the divisor may be zero
Examples
Division by zero
FeatureQL
SELECT
f1 := TRY_DIVIDE(10E0, 3E0), -- Division with remainder
f2 := TRY_DIVIDE(15E0, 5E0), -- Exact division
f3 := TRY_DIVIDE(-8E0, 4E0) -- Negative division
;Result
| f1 VARCHAR | f2 VARCHAR | f3 VARCHAR |
|---|---|---|
| 3.3333333333333335 | 3.0 | -2.0 |