Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Database > SQL Language
|
SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Language section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 20th, 2003, 02:15 PM
Authorized User
 
Join Date: Jun 2003
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Default Min of list of variables

Hello-

I am writing a stored proc in which I need to take the minimum of a list of variables that are set previously in the proc.

For example, if I have variables @num1, @num2, @num3, and @minNumber, what can I do to put the smallest value out of @num1, @num2, and @num3 into @minNumber?

Any help would be much appreciated.

Thanks,
Mike
 
Old June 20th, 2003, 02:45 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

Bad news.

I can only speak to SQL Server, but there is no simple, easy way to do this. There is no MIN function which will return the minimum of two values. The MIN is an SQL aggregate function which returns the minimum value in a set of rows.

If you are using SQL Server 2k, you could write a user defined function which would return the minimum of its two arguments, then add code to the stored procedure like:
Code:
   set @minNumber=MyMin(@num1,@num2)
   set @minNumber=MyMin(@minNumber,@num3)
   ...
You could create a temporary table (in SQL Server 2K this can be an in-memory table variable) containing the values you wish to search and use the MIN aggregate to find the minimum of them all:
Code:
   DECLARE @MyTable table(NumberColumn int not null)
   INSERT @MyTable (Numbercolumn) VALUES (@num1)
   INSERT @MyTable (Numbercolumn) VALUES (@num2)
   ... 
   SELECT @minNumber=MIN(numbercolumn) FROM @MyTable;
There are other ways involving a series of IF statements or CASE Expressions which may be simpler and quicker...

Jeff Mason
Custom Apps, Inc.
www.custom-apps.com





Similar Threads
Thread Thread Starter Forum Replies Last Post
MAX/MIN Value Help slbibs SQL Server 2005 4 November 6th, 2007 04:00 AM
Min Function deontae45 SQL Server 2000 13 September 23rd, 2006 05:45 AM
Load two variables into Dropdown list tryntolearn Classic ASP Databases 2 August 12th, 2005 09:25 AM
min-height in IE cgallagr98 CSS Cascading Style Sheets 5 April 8th, 2005 12:47 PM
MIN() function mohiddin52 Access 1 October 29th, 2004 07:09 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.