SQLUSA.com
SQL SERVER 2008 GRAND SLAM
FREE TRIAL  CLICK HERE TO ORDER  SEARCH
SQL JOBS SQL Server Training Scripts JOB BANK
  SQL Server 2008 Training Scripts  
  SQL Server 2005 Training Scripts  
News SQL Server Articles SQL Format

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)

 

 

Order SQL 2008 GRAND SLAM Today!
SQLUSA.com Home Page
SQL Server Training at www.sqlusa.com.
SQL Server 2008 Video Training at www.sqlusa.com.
SQL Server 2005 Training Videos at www.sqlusa.com.
Microsoft SQL Server 2000 Training Videos at www.sqlusa.com.

FREE SQL & Business Intelligence / OLAP Short Videos on YOUTUBE.com

Microsoft Community Contributor 2011
Invest in Your SUCCESS!

Search SQLUSA FREE SQL Server Articles & FREE T-SQL Scripts


Copyright 2005-2011, SMI Corp. All Rights Reserved.

SQL Server 2012 is a program product of Microsoft Corporation.
SQL Server 2008 is a program product of Microsoft Corporation.
SQL Server 2005 is a program product of Microsoft Corporation.
SQL Server 2000 is a program product of Microsoft Corporation.