DATETIME SELECT SELECT INTO DATE PAD STRING DYNAMIC SQL CURSOR MONEY FORMAT PERCENT STORED PROCEDURE SQL SERVER AGENT JOB OPTIMIZATION WHILE LOOP OVER PARTITION BY UPDATE
SITE SEARCH SQLUSA.com HEADLINES NEWS
SQL E/BOOKS   SQL 2014 PROGRAMMING   DOWNLOADS
SCRIPTS SQL 2005 SQL 2008 ARTICLES
SQL JOBS TWITTER FORMAT VIDEOS
How to find the minimum of two numbers?

Execute the following T-SQL example scripts in Microsoft SQL Server Management Studio Query Editor for minimum calculations.

USE AdventureWorks2008;
GO
-- SQL minimum of 2 values
-- SQL UDF - scalar-valued function - user-defined function
CREATE FUNCTION fnMinValue
               (@ColumnA MONEY,
                @ColumnB MONEY)
RETURNS MONEY
AS
  BEGIN
    RETURN (0.5 * ((@ColumnA + @ColumnB) - abs(@ColumnA - @ColumnB)))
  END
GO
 
-- Test UDF
SELECT dbo.fnMinValue ( 1000.0, 1001.0)
GO
-- 1000.00
 
SELECT dbo.fnMinValue ( 1002.0, 1001.0)
GO
-- 1001.00
 
SELECT dbo.fnMinValue ( 1003.0, 1003.0)
GO
-- 1003.00
 ------------

 

-- Minimum values by group

SELECT Color, MinListPrice=MIN(ListPrice)

FROM AdventureWorks2008.Production.Product

GROUP BY Color

/* Color          MinListPrice

NULL              0.00

Black             0.00

Blue              34.99

Grey              125.00

Multi             8.99

Red               34.99

Silver            0.00

Silver/Black      40.49

White             8.99

Yellow            53.99  */

------------

 

Related articles:

 

What's the best way to select the minimum value from multiple columns?

 

MIN (Transact-SQL)

 

 

Exam Prep 70-461
Exam 70-461
DATETIME SELECT SELECT INTO DATE PAD STRING DYNAMIC SQL CURSOR MONEY FORMAT PERCENT STORED PROCEDURE SQL SERVER AGENT JOB OPTIMIZATION WHILE LOOP OVER PARTITION BY UPDATE