Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Problem with scalar Function

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
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,

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

52326 Posts

Posted - 2012-03-12 : 09:59:28
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
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2012-03-12 : 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.
Go to Top of Page

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 MVP
http://visakhm.blogspot.com/

Go to Top of Page

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.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
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.
Go to Top of Page

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 MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -