Wednesday, December 4, 2013

Finding maximum value out of different columns

DECLARE @a INT, @b INT, @c INT 

SET @a = 10 
SET @b = 7 
SET @c = 12 

--sql server 2008 and above
SELECT MAX(v) AS MaxVal 
FROM   ( VALUES (@a), 
                (@b), 
                (@c) ) AS value(v) 

--sql server 2005
SELECT MAX(v) AS MaxVal 
FROM   (SELECT @a UNION 
        SELECT @b UNION 
        SELECT @c) AS value(v)

No comments: