Categories
Sql Server

How To Compare 3 Numbers Using TSQL In SQL Server

How To Compare 3 Numbers Using TSQL In SQL Server

In this tutorial we will learn How To Compare 3 Numbers Using TSQL In SQL Server. Below sql codes easily sort 3 numbers given as variables from biggest and smallest values.

First declares variables for input values. These numeric variables are declared in int data type with variable names @v1, @v2 and @v3.

DECLARE @V1 INT, @V2 INT, @V3 INT
DECLARE @SMAX INT, @SMIN INT
SELECT @V1 = 1, @V2 = 10, @V3 = 2
IF @V1 > @V2
SELECT @SMAX = @V1, @SMIN = @V2
ELSE
SELECT @SMAX = @V2, @SMIN = @V1
IF @V3 < @SMIN
SELECT @SMIN = @V3
ELSE IF @V3 > @SMAX
SELECT @SMAX = @V3
SELECT @SMAX AS 'MAX', @SMIN AS 'MIN'