Guest

T-SQL equivalent of Excel “MAX” function to return larger of two numbers

By: a guest on Jan 28th, 2012  |  syntax: None  |  size: 0.41 KB  |  hits: 23  |  expires: Never
download  |  raw  |  embed  |  report abuse
Copied
  1. CREATE FUNCTION dbo.LargerOf
  2. (
  3.     -- Add the parameters for the function here
  4.     @First FLOAT,
  5.     @Second FLOAT
  6. )
  7. RETURNS FLOAT
  8. AS
  9. BEGIN
  10.  
  11.     DECLARE @Result FLOAT
  12.  
  13.     IF @First > @Second
  14.         SET @result = @First
  15.     ELSE
  16.         SET @Result = @Second
  17.  
  18.     RETURN @Result
  19.  
  20. END
  21. GO
  22.        
  23. set @max = case when @first > @second then @first else @second end
  24.        
  25. CASE
  26.    WHEN @First >= @Second THEN @FIRST
  27.    ELSE @Second
  28. END