Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Database > SQL Language
Password Reminder
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old June 20th, 2003, 02:15 PM
Authorized User
Join Date: Jun 2003
Location: , , .
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Default Min of list of variables


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.

Reply With Quote
  #2 (permalink)  
Old June 20th, 2003, 02:45 PM
Friend of Wrox
Join Date: Jun 2003
Location: Hudson, MA, USA.
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post

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:
   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:
   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.
Reply With Quote

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

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

All times are GMT -4. The time now is 09:07 PM.

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