SQL SERVER – T-SQL Scripts to Find Maximum between Two Numbers
I found there is a simple way to write query to get the maximum of two numbers in SQL Server.
Method 1:
DECLARE @Value1 DECIMAL(5,2) = 9.22
DECLARE @Value2 DECIMAL(5,2) = 8.34
SELECT (0.5 * ((@Value1 + @Value2) + ABS(@Value1 - @Value2))) AS MaxColumn
I thought his logic was accurate but the same script can be written another way. I quickly wrote following code for him and which worked just fine for him.
Method 2:
DECLARE @Value1 DECIMAL(5,2) = 9.22
DECLARE @Value2 DECIMAL(5,2) = 8.34
SELECT CASE WHEN @Value1 > @Value2 THEN @Value1 ELSE @Value2 END AS MaxColumn
For me Method 2 is looks simpler than Method 1.
Method 1:
DECLARE @Value1 DECIMAL(5,2) = 9.22
DECLARE @Value2 DECIMAL(5,2) = 8.34
SELECT (0.5 * ((@Value1 + @Value2) + ABS(@Value1 - @Value2))) AS MaxColumn
I thought his logic was accurate but the same script can be written another way. I quickly wrote following code for him and which worked just fine for him.
Method 2:
DECLARE @Value1 DECIMAL(5,2) = 9.22
DECLARE @Value2 DECIMAL(5,2) = 8.34
SELECT CASE WHEN @Value1 > @Value2 THEN @Value1 ELSE @Value2 END AS MaxColumn
For me Method 2 is looks simpler than Method 1.
Comments