Author |
Topic |
a4u6178
Starting Member
11 Posts |
Posted - 2012-03-12 : 06:16:18
|
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 BEGINDECLARE @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 @comissionEND calling function:DECLARE @result DECIMALset @result=select dbo.calculateComission('20','29.01','4.5') as Resultselect @resultAny Help is much appreciated!Thanks & Regards, |
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2012-03-12 : 06:41:58
|
quote: Originally posted by a4u6178 Hi all,I am trying to calculate percentage value using scalar function it returns NULL resultHere is the function:alter Function calculateComission(@discount nvarchar(25),@regFEE varchar(25),@comissionPercentage nvarchar(25))RETURNS varchar(25)ASBEGINDECLARE @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 @comissionENDcalling function:DECLARE @result DECIMALset @result=select dbo.calculateComission('20','29.01','4.5') as resultprint @resultThanks & Regards,
No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2012-03-12 : 10:27:22
|
quote: Originally posted by visakh16 reason is thishttp://sqlblogcasts.com/blogs/madhivanan/archive/2008/01/16/beware-of-implicit-conversions.aspx------------------------------------------------------------------------------------------------------SQL Server MVPhttp://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. |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-03-12 : 10:48:59
|
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 MVPhttp://visakhm.blogspot.com/ |
 |
|
DonAtWork
Master Smack Fu Yak Hacker
2167 Posts |
Posted - 2012-03-12 : 10:52:32
|
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.aspxHow to ask: http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxFor ultra basic questions, follow these links.http://www.sql-tutorial.net/ http://www.firstsql.com/tutor.htm http://www.w3schools.com/sql/default.asp |
 |
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2012-03-12 : 11:03:53
|
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. |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-03-12 : 11:27:42
|
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 MVPhttp://visakhm.blogspot.com/ |
 |
|
|