Solved

Writing Functions in SQL

Posted on 2009-05-07
2
190 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
Comment Utility
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 40

Expert Comment

by:Sharath
Comment Utility
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

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

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…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This video discusses moving either the default database or any database to a new volume.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

744 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now