| Author |
Topic  |
|
|
a4u6178
Starting Member
India
11 Posts |
Posted - 03/12/2012 : 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 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
8513 Posts |
Posted - 03/12/2012 : 06:41:58
|
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. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
India
47069 Posts |
|
|
webfred
Flowing Fount of Yak Knowledge
Germany
8513 Posts |
Posted - 03/12/2012 : 10:27:22
|
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. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
India
47069 Posts |
Posted - 03/12/2012 : 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 MVP http://visakhm.blogspot.com/
|
 |
|
|
DonAtWork
Flowing Fount of Yak Knowledge
2111 Posts |
|
|
webfred
Flowing Fount of Yak Knowledge
Germany
8513 Posts |
Posted - 03/12/2012 : 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
India
47069 Posts |
Posted - 03/12/2012 : 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 MVP http://visakhm.blogspot.com/
|
 |
|
| |
Topic  |
|