0

I need to create function that will find a lesser of two values. It should receive either INT or TIME type arguments. Something of this structure:

CREATE FUNCTION fn_Min(@p_1 (time or int), @p_2 (time or int))
RETURNS time
AS
BEGIN
    DECLARE @lesser (time or int)
    IF @p_1 <= @p_2
        SET @lesser = @p_1
    ELSE 
        SET @lesser = @p_2
    RETURN @lesser  
END

Is it possible?

1 Answer 1

1

you can use sql_variant

Function

CREATE FUNCTION fn_Min(@p_1 sql_variant, @p_2 sql_variant)
RETURNS sql_variant
AS
BEGIN
    DECLARE @lesser sql_variant
    IF @p_1 <= @p_2
        SET @lesser = @p_1
    ELSE 
        SET @lesser = @p_2
    RETURN @lesser  
END

Call

SELECT dbo.fn_Min(CAST('3:00 AM' AS TIME),CAST('11:00 AM' AS TIME))
SELECT dbo.fn_Min(5,6)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.