Solved

Writing Functions in SQL

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

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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

I am showing a way to read/import the excel data in table using SQL server 2005... Suppose there is an Excel file "Book1" at location "C:\temp" with column "First Name" and "Last Name". Now to import this Excel data into the table, we will use…
In this article I will describe the Backup & Restore 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 tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

803 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