Click here to Skip to main content
15,899,124 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Basically, I have two columns i shall be working with..

First column is SalaryAmount, then the second column is AmountPerHour. Now SalaryAmount is an int and AmountPerHour is a decimal.


But the AmountPerHour is based on the calculation of the SalaryAmount for example...

Here is the SQL query i have for the AmountPerHour..

SQL
UPDATE Salary SET SalaryAmount=@SalaryAmount,
                AmountPerHour= round((@SalaryAmount / 52 /48), 2, 4)


But the real answer should be 12.02, but it comes out as 12.00. Not im not sure if this is because of the rounding. or because of the fact that you can't calculate an int number and want it to come out as a decimal?


So my question, is that is there a way where i can convert the SalaryAmount to a decimal just for one query, and then change it back at the end? Therefore the calcuation is done correctly and the number isnt rounded off.

Please dont say change the salaryamount to decimal altogether because that messes alot with the code i have from other projects as they all use the same database.
Posted
Updated 16-Apr-14 8:29am
v2

Yes:
SQL
UPDATE Salary SET SalaryAmount=@SalaryAmount,
                AmountPerHour= round((CAST(@SalaryAmount AS DECIMAL(18,0)) / 52.0 /48.0), 2, 4)
 
Share this answer
 
v2
Comments
[no name] 16-Apr-14 14:31pm    
I love you, thank you, you have literally saved me hours off troubleshooting..

I do ask, can you explain to me how that works? I like to learn from my mistakes :) Thank you.
OriginalGriff 16-Apr-14 14:34pm    
All it does is cast the one value to a double from it's int - which means that the result of the whole calculation is a double as well. So the intermediate fractional parts aren't thorwn away, is all.
[no name] 16-Apr-14 14:35pm    
Perfect.. Thank you buddy :)
Convert @SalaryAmount to decimal before doing this calculation.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900