Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# Writing Functions in SQL

Posted on 2009-05-07
Medium Priority
197 Views

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
``````
0
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

LVL 75

Accepted Solution

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

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
``````
0

## Featured Post

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…
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.
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
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…
###### Suggested Courses
Course of the Month7 days, 12 hours left to enroll