Avatar of hidrau
hidrauFlag for Brazil

asked on 

Help on my function to get a percentual from two values

Hello guys

I made this function to get the percentual from two value, but sometimes it works fine and sometimes it doesn't.

could you help me to ajust it?

Thanks a lot

alter Function  fn_PercentualDesconto (@Fullvalue float, @EndValue float)
  Returns Float
Begin 
	declare @v01 float;
	declare @v02 float;
	declare @v03 float;

        Set @v01=@Fullvalue 
	Set @v02=@EndValue 
	Set @v03=(@v01-@v02)
	Return 100- round((@v03 / @v01 ) * 100 , 2) 
End

Open in new window

Microsoft SQL Server 2008

Avatar of undefined
Last Comment
Pavel Celba
Avatar of David Johnson, CD
David Johnson, CD
Flag of Canada image

replace Return 100- round((@v03 / @v01 ) * 100 , 2)
with
Return 100.00- round((@v03 / @v01 ) * 100.00, 2)

Open in new window

100 is an integer
Avatar of Pavel Celba
Pavel Celba
Flag of Czechia image

If you are sure the @Fullvalue can never be 0 then calculate the result as
alter Function  fn_PercentualDesconto (@Fullvalue float, @EndValue float)
  Returns Float
Begin 
	Return round( (@EndValue / @Fullvalue) * 100, 2) 
End

Open in new window

If the possibility to have @Fullvalue = 0.0 exists then add one condition:
Return CASE WHEN @Fullvalue = 0.0 THEN 0.00 ELSE round( (@EndValue / @Fullvalue) * 100, 2) END

Open in new window

Or generate some error.

BTW, I don't understand your sentence "sometimes it works fine and sometimes it doesn't." .  Could you provide some values which do not work?
Avatar of hidrau
hidrau
Flag of Brazil image

ASKER

Hello Guys,

Thanks a lot for helping me

But Something wrong here, take a look:

When I do this:

select dbo.fn_PercentualDesconto(60, 54)

it returns me 90, it is wrong because it should be 10%  

60 is my full value and I reached the 54 - The discount that I have was 6

6 on the 60 represents 10% and not 90%

thanks
Avatar of Steve Bink
Steve Bink
Flag of United States of America image

        Set @v01=@Fullvalue 
	Set @v02=@EndValue 
	Set @v03=(@v01-@v02)
	Return 100- round((@v03 / @v01 ) * 100 , 2) 

Open in new window


Executes as:
Set @v01 = 60
Set @v02 = 54
Set @v03 = (60-54)   <--  @v03=6
Return 100 - round (6 / 60 * 100, 2)   <-- return 100 - 10, or 90

Open in new window

Sounds like your code is doing exactly what you told it to.
Avatar of hidrau
hidrau
Flag of Brazil image

ASKER

Steve Bink,

I think my code is not correct.

See:

I bought a book for $ 100, its normal price was $ 112.

When I was going to pay it, I just payed $100 becouse I had a discount on it.

It is not working at any way :(
What was my discount?

Select dbo.fn_PercentualDesconto(112, 100)

The function returned me: %89,29   and it is not correct, it should be 10% of discount.
ASKER CERTIFIED SOLUTION
Avatar of Steve Bink
Steve Bink
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of hidrau
hidrau
Flag of Brazil image

ASKER

thanks guys for the help
Avatar of Pavel Celba
Pavel Celba
Flag of Czechia image

So you now understand percent value vs. percent discount hopefully.

You could simply use my formula slightly updated for discount calculation:

Return round( (1-(@EndValue / @Fullvalue)) * 100, 2)
Microsoft SQL Server 2008
Microsoft SQL Server 2008

Microsoft SQL Server 2008 is a suite of relational database management system (RDBMS) products providing multi-user database access functionality.Component services include integration (SSIS), reporting (SSRS), analysis (SSAS), data quality, master data, T-SQL and performance tuning. Major improvements include the Always On technologies and support for unstructured data types.

50K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo