[英]SQL Server: update a field with divided and multiplied values from other field on the same table
I want to update a field from my table with a value that comes from: Transaction_Count field : 10 x 100 For example the Transaction_Count value: 3. Then the calculation should be: 3 : 10 x 100 = 30 我想用来自以下表的值更新表中的字段:Transaction_Count字段:10 x 100例如,Transaction_Count值:3。那么计算应为:3:10 x 100 = 30
But when I run the code, the result is 0 without leaving error message. 但是,当我运行代码时,结果为0而没有留下错误消息。 As additional information I already created Support field of my Mining table with decimal(18,2) data type. 作为附加信息,我已经使用十进制(18,2)数据类型创建了挖掘表的Support字段。 How to get the correct result, can anybody help me? 如何获得正确的结果,有人可以帮助我吗?
Here's my code: 这是我的代码:
SQL = "Update Mining Set Support = Transaction_Count / 10 * 100"
Con.Execute (SQL)
SQL Server, as you have discovered, does integer division. 您已经发现,SQL Server执行整数除法。 You can just rearrange the operands: 您可以重新排列操作数:
Update Mining
Set Support = (100 * Transaction_Count) / 10;
Your expression was being evaluated as: 您的表情被评估为:
(Transaction_Count / 10) * 100 = (3 / 10) * 100
The 3 / 10
is 0
(and NOT 0.33333333) in SQL Server, because it does integer division. 的3 / 10
是0
在SQL Server(和NOT 0.33333333),因为它确实整数除法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.