Consider the following function
CREATE FUNCTION dbo.SafeDivide
(
@a numeric,
@b numeric
)
RETURNS numeric
AS
BEGIN
RETURN CASE
WHEN @b = 0
THEN 0
ELSE @a / @b
END;
END;
Here type numeric was used but this code would be useful for any numeric types with any precision.
Is there a way to define and use such function?
I tried sql_variant type but it complains
Operand data type sql_variant is invalid for divide operator