I have been given three values: @lLength, @lWidth, @lHeight. I need to take these DECIMAL(4,2) values and set the values of @lMax, @lMid, @lMin with the values I've been given in order of value. So whichever value of Length, Width or Height is the greatest will be stored in Max, and the second created in Mid, and the lowest value in Min.Note: I am limited to Microsoft SQL Server 2008.-- Declarations are for testing purposes onlyDECLARE @lLength DECIMAL(4,2) = CAST('1.0' AS DECIMAL(4,2))DECLARE @lWidth  DECIMAL(4,2) = CAST('2.0' AS DECIMAL(4,2))DECLARE @lHeight DECIMAL(4,2) = CAST('3.0' AS DECIMAL(4,2))DECLARE @lMax    DECIMAL(4,2)DECLARE @lMid    DECIMAL(4,2)DECLARE @lMin    DECIMAL(4,2)--Determine which is higher and store it.IF @lLength > @lWidth BEGIN    SET @lMax = @lLength    SET @lMid = @lWidthEND ELSE BEGIN    SET @lMax = @lWidth    SET @lMid = @lLengthEND--Now determine where the 3rd value needs to be insertedIF @lHeight < @lMid BEGIN    SET @lMin = @lHeightEND ELSE IF @lHeight < @lMax BEGIN    SET @lMin = @lMid    SET @lMid = @lHeightEND ELSE BEGIN    SET @lMin = @lMid    SET @lMid = @lMax    SET @lMax = @lHeightEND--This select is for testing purposes onlySELECT @lMax as MAX, @lMid AS MID, @lMin AS MINThe code above works, and I added declarations and a select to make that readily apparent. I am looking for a general review of this operation, perhaps a better way to do this. You can criticize the naming convention, but that is something I have no control over. Also, I do not have to worry about scalability of this operation unless our world gets a 4th physical dimension in the near future.
Sorting three Decimal values
sql;sorting;sql server;t sql
You can use the variables directly in a table value constructor.SELECT @lMin = CASE WHEN RN = 1 THEN Size ELSE @lMin END,       @lMid = CASE WHEN RN = 2 THEN Size ELSE @lMid END,       @lMax = CASE WHEN RN = 3 THEN Size ELSE @lMax ENDFROM   (SELECT Size,               ROW_NUMBER() OVER (ORDER BY Size) AS RN        FROM   (VALUES (@lLength),                       (@lWidth),                       (@lHeight)) V(Size)) T SQL Fiddle