# 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
``````
Microsoft SQL Server 2008

Last Comment
Pavel Celba
David Johnson, CD

replace Return 100- round((@v03 / @v01 ) * 100 , 2)
with
``````Return 100.00- round((@v03 / @v01 ) * 100.00, 2)
``````
100 is an integer
Pavel Celba

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
``````
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
``````
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?
hidrau

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

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

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
``````
Sounds like your code is doing exactly what you told it to.
hidrau

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.
Steve Bink

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

thanks guys for the help
Pavel Celba

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

TRUSTED BY