Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Writing Functions in SQL

Posted on 2009-05-07
2
Medium Priority
?
198 Views
Last Modified: 2012-06-21

5) Write a function named min that takes three int values as arguments, and returns the smallest.

How do I get the smallest number using the following Function
CREATE FUNCTION dbo.min (
 
@num1 int,
 
@num2 int,
 
@num3 int )
 
Returns int
 
as 
 
begin 
 
return

Open in new window

0
Comment
Question by:iolike
2 Comments
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 2000 total points
ID: 24333439
CREATE FUNCTION dbo.minimum (
@num1 int,
@num2 int,
@num3 int )
Returns int
as
begin
      declare @min int
      SELECT @Min = CASE WHEN @num1 < @num2 THEN @num1 ELSE @num2 END
      SELECT @Min = CASE WHEN @Min< @num3 THEN @Min ELSE @num3 END
      RETURN (@min)
END

go
0
 
LVL 41

Expert Comment

by:Sharath
ID: 24333550
this will also do.
call this function like this. as you want the function name as MIN, you need to use [ and ] as MIN is key word.

select dbo.[min](1,5,3)

CREATE FUNCTION dbo.[min] (
@num1 int,
@num2 int,
@num3 int )
Returns int
as 
begin
      declare @table table(num int)
      declare @min int
      insert @table values (@num1)
      insert @table values (@num2)
      insert @table values (@num3)
      select @min = min(num) from @table
      return (@min)
end
go 

Open in new window

0

Featured Post

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing XML code a very difficult part is when we like to remove all the elements or attributes from the XML that have no data. I would like to share a set of recursive MSSQL stored procedures that I have made to remove those elements from …
Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question