如何将Int转换为一个查询的小数 [英] How To I Convert An Int To A Decimal For One Query
问题描述
基本上,我有两列我将要合作..
第一列是SalaryAmount,第二列是AmountPerHour。现在SalaryAmount是一个int,AmountPerHour是一个小数。
但AmountPerHour基于SalaryAmount的计算,例如.. 。
这是我对AmountPerHour的SQL查询..
更新薪资 SET SalaryAmount = @ SalaryAmount,
AmountPerHour = round(( @ SalaryAmount / 52 / 48), 2 , 4 )
但真正的答案应该是12.02,但它来了出12.00。我不确定这是否是因为四舍五入。或者因为你无法计算一个int数并希望它以小数形式出现?
所以我的问题,是否有一种方法可以将SalaryAmount转换为仅为一个查询的十进制,然后在结束时将其更改回来?因此,计算正确完成并且数字没有四舍五入。
请不要说完全将salaryamount更改为十进制因为这会使我从其他项目中获得的代码变得很糟糕因为他们都使用相同的数据库。
是:
更新薪水 SET SalaryAmount = @ SalaryAmount,
AmountPerHour = round((CAST( @SalaryAmount AS DECIMAL ( 18 , 0 ))/ 52 。 0 /48. 0 ), 2 , 4 )
在执行此计算之前将 @SalaryAmount
转换为十进制Ñ
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..
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.
Yes:
UPDATE Salary SET SalaryAmount=@SalaryAmount, AmountPerHour= round((CAST(@SalaryAmount AS DECIMAL(18,0)) / 52.0 /48.0), 2, 4)
Convert @SalaryAmount
to decimal before doing this calculation.
这篇关于如何将Int转换为一个查询的小数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!