TSQL -> User defined function that returns biggest of two numbers

User defined function that returns biggest of two numbers.

IF EXISTS (SELECT * FROM sys.objects
  WHERE object_id = OBJECT_ID (N'[dbo].[udf_max_value]')
  AND type IN (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[udf_max_value]
GO

CREATE FUNCTION dbo.udf_max_value (@n1 FLOAT, @n2 FLOAT) RETURNS FLOAT
AS

BEGIN
-- returns biggest of two numbers or NULL if input arguments are NULL
RETURN CASE
  WHEN
(@n1 >= @n2) THEN @n1
  WHEN (@n2 > @n1) THEN @n2
ELSE 0 END;

END
GO


Test function by selecting values from Temporary table:
DECLARE @test_numbers TABLE (n1 FLOAT, n2 FLOAT);

INSERT INTO @test_numbers (n1, n2)
VALUES (1, 2.3), (0.99, 0.0008), (14.8, 75);

SELECT dbo.udf_max_value(n1, n2) AS Max_Val
FROM @test_numbers;


Results:
Max_Val
2.3
0.99
75



sqlexamples.info