Solved

Writing Functions in SQL

Posted on 2009-05-07
2
195 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 500 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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

So every once in a while at work I am asked to export data from one table and insert it into another on a different server.  I hate doing this.  There's so many different tables and data types.  Some column data needs quoted and some doesn't.  What …
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

617 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