SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Problem with scalar Function
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

a4u6178
Starting Member

India
11 Posts

Posted - 03/12/2012 :  06:16:18  Show Profile  Reply with Quote
Hi all,

I am trying to calculate percentage value using scalar function it returns 1 result instead of 1.044 how do achieve correct value

Here is the function:

alter Function calculateComission
(@discount nvarchar(25),
@regFEE varchar(25),
@comissionPercentage nvarchar(25))
RETURNS varchar(25)
AS
BEGIN

DECLARE @dicountedREGFEE DECIMAL,@comission DECIMAL;

set @dicountedREGFEE=((cast(@discount as DECIMAL(18,2)) *cast(@regFEE as DECIMAL(18,2)))/100);
set @comission=((cast(@comissionPercentage as DECIMAL(18,2))*((cast(@regFEE as DECIMAL(18,2))- @dicountedREGFEE)))/100);

return @comission
END

calling function:

DECLARE @result DECIMAL
set @result=select dbo.calculateComission('20','29.01','4.5') as Result
select @result

Any Help is much appreciated!
Thanks & Regards,

Edited by - a4u6178 on 03/12/2012 06:48:10

webfred
Flowing Fount of Yak Knowledge

Germany
8760 Posts

Posted - 03/12/2012 :  06:41:58  Show Profile  Visit webfred's Homepage  Reply with Quote
quote:
Originally posted by a4u6178

Hi all,

I am trying to calculate percentage value using scalar function it returns NULL result

Here is the function:

alter Function calculateComission
(@discount nvarchar(25),
@regFEE varchar(25),
@comissionPercentage nvarchar(25))
RETURNS varchar(25)
AS
BEGIN

DECLARE @dicountedREGFEE DECIMAL,@comission DECIMAL;

set @dicountedREGFEE=((cast(@discount as DECIMAL(18,2)) *cast(@regFEE as DECIMAL(18,2)))/100);
set @comission=((@dicountedREGFEE * cast(@comissionPercentage as DECIMAL(18,4)))/100);

return @comission
END

calling function:

DECLARE @result DECIMAL
set @result=select dbo.calculateComission('20','29.01','4.5') as result
print @result

Thanks & Regards,




No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52309 Posts

Posted - 03/12/2012 :  09:59:28  Show Profile  Reply with Quote
reason is this

http://sqlblogcasts.com/blogs/madhivanan/archive/2008/01/16/beware-of-implicit-conversions.aspx

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

webfred
Flowing Fount of Yak Knowledge

Germany
8760 Posts

Posted - 03/12/2012 :  10:27:22  Show Profile  Visit webfred's Homepage  Reply with Quote
quote:
Originally posted by visakh16

reason is this

http://sqlblogcasts.com/blogs/madhivanan/archive/2008/01/16/beware-of-implicit-conversions.aspx

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/




Dear Visakh,
in this case it is not the reason!
I agree it is a messy function but implicit conversion is not the reason.
The real reason is a NULL value because of accidently using the wrong variable as pointed out in red!
So feel free to always post one on top when an answer is already given - but do it right.


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52309 Posts

Posted - 03/12/2012 :  10:48:59  Show Profile  Reply with Quote
From what I can see OP is also using the same variable you pointed out. May be he has edited the post after your reply. Also he has still not confirmed that its working fine. Thats why I didnt pick it up!

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

DonAtWork
Flowing Fount of Yak Knowledge

2161 Posts

Posted - 03/12/2012 :  10:52:32  Show Profile  Reply with Quote
Yep, when you compare the quote with the OP, you can see editing has happened.

http://weblogs.sqlteam.com/jeffs/archive/2008/05/13/question-needed-not-answer.aspx
How to ask: http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

For ultra basic questions, follow these links.
http://www.sql-tutorial.net/
http://www.firstsql.com/tutor.htm
http://www.w3schools.com/sql/default.asp
Go to Top of Page

webfred
Flowing Fount of Yak Knowledge

Germany
8760 Posts

Posted - 03/12/2012 :  11:03:53  Show Profile  Visit webfred's Homepage  Reply with Quote
OMG - I am sorry!
It is not only a messy style of coding functions, it is also a messy style to post in a thread.

Again sorry Visakh


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52309 Posts

Posted - 03/12/2012 :  11:27:42  Show Profile  Reply with Quote
quote:
Originally posted by webfred

OMG - I am sorry!
It is not only a messy style of coding functions, it is also a messy style to post in a thread.

Again sorry Visakh


No, you're never too old to Yak'n'Roll if you're too young to die.


Thats ok Fred

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.09 seconds. Powered By: Snitz Forums 2000