Solved

Writing Functions in SQL

Posted on 2009-05-07
2
194 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 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Pivot Query Problem 9 51
MS SQL Server - Looking to filter rows based on column value 3 56
Need help how to find where my error is in UFD 6 47
Sql Server group by 10 52
There are some very powerful Data Management Views (DMV's) introduced with SQL 2005. The two in particular that we are going to discuss are sys.dm_db_index_usage_stats and sys.dm_db_index_operational_stats.   Recently, I was involved in a discu…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.

696 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