Generic type functions in T-SQL 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
The error says what to do, that sql_variant type does not accept to operate the divide function.

I would suggest to use CONVERT/CAST to a specific data type, depending on your usage.
Below I will use float:

DROP FUNCTION IF EXISTS dbo.SafeDivide
GO
CREATE FUNCTION dbo.SafeDivide
(
    @a sql_variant,
    @b sql_variant
)
RETURNS sql_variant
AS
BEGIN
     RETURN CASE
               WHEN @b = 0
                   THEN 0.00
               ELSE Convert(float, @a) / Convert(float, @b) 
           END;
END
GO
/*
select dbo.SafeDivide(4,4.4622434)
*/