try_divide function
Applies to: Databricks SQL
Databricks Runtime 10.4 LTS and above
Returns dividend divided by divisor.
Syntax
try_divide(dividend, divisor)
Arguments
dividend: A numeric orINTERVALexpression.divisor: A numeric expression.
Returns
For DECFLOAT inputs, the result is DECFLOAT(16) when both inputs are DECFLOAT(16), and DECFLOAT(34) otherwise. Division by zero produces infinity or NaN rather than NULL.
If both dividend and divisor are DECIMAL, the result is DECIMAL.
If dividend is a year-month interval, the result is an INTERVAL YEAR TO MONTH.
If dividend is a day-time interval, the result is an INTERVAL DAY TO SECOND.
In all other cases, a DOUBLE.
For inputs other than DECFLOAT, if the divisor is 0, the operator returns NULL.
Examples
SQL
> SELECT try_divide(3, 2);
1.5
> SELECT try_divide(2L, 2L);
1.0
> SELECT try_divide(INTERVAL '3:15' HOUR TO MINUTE, 3);
0 01:05:00.000000
> SELECT try_divide(3 , 0)
NULL