Click here to Skip to main content
15,881,898 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
I need to create a user defined function to find simple Interest, with Principal amount, no. of years and rate as parameters.
The next one is to create a user defined function to find the factorial of a number.
Help me out...
Posted
Comments
Tim Corey 8-Oct-12 8:47am    
"Help me out" isn't exactly a question. We aren't here to do your work. What have you tried? Do you know what a UDF is? Have you figured out how to do a query for this? Sorry, right now this looks to be a "do my work for me" post and I don't respond well to that.
Herman<T>.Instance 8-Oct-12 8:58am    
We can give him a google search key like this one.

1 solution

SQL
create FUNCTION dbo.Factorial ( @iNumber bigint )
RETURNS INT
AS
BEGIN
DECLARE @i  bigint

    IF @iNumber <= 1
        SET @i = 1
    ELSE
        SET @i = @iNumber * dbo.Factorial( @iNumber - 1 )
RETURN (@i)
END


declare @int bigint
exec @int= dbo.Factorial 12
select @int
 
Share this answer
 
Comments
bolshie6 9-Oct-12 1:10am    
This is my UDF for finding Simple interest. Theres always an error near RETURN

CREATE FUNCTION ufn_SimpleInterest (@Principal NUMERIC(19,4), @TimePeriod NUMERIC(19,2), @Rate NUMERIC(19,9)
RETURN NUMERIC(19,4)
AS
BEGIN
DECLARE @SimpleInterest NUMERIC(19,4)
SELECT @SimpleInterest = ( @Principal * @TimePeriod * @Rate) / 100
RETURN @SimpleInterest
END
Santhosh Kumar Jayaraman 9-Oct-12 1:11am    
try this
CREATE FUNCTION ufn_SimpleInterest (@Principal NUMERIC(19,4), @TimePeriod NUMERIC(19,2), @Rate NUMERIC(19,9))
RETURNs INT
AS
BEGIN
DECLARE @SimpleInterest NUMERIC(19,4)
SELECT @SimpleInterest = ( @Principal * @TimePeriod * @Rate) / 100
RETURN @SimpleInterest
END
bolshie6 9-Oct-12 1:18am    
Thank you, It works. I've noted that its 'RETURNS'.
Santhosh Kumar Jayaraman 9-Oct-12 1:20am    
also u missed closing bracket next to @Rate NUMERIC(19,9)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900