CREATE FUNCTION [dbo].[DATETIME_LEAST_2PARAM]
(@val1 DATETIME, @val2 DATETIME)
returns DATETIME
AS
BEGIN
    Declare @result DATETIME
    set @result = case when @val1 < @val2 then
                @val1
               else
                @val2
               end
    return @result
END