Sql round函数没有按预期工作 [英] Sql round function not working as expected

查看:99
本文介绍了Sql round函数没有按预期工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello All,



我正在努力将MS Access宏转换为sql,

虽然我转换我面临一个严重的问题。



虽然ms访问和sql是Microsoft产品转换号码的工作方式不同。

Ex。

IN MS ACCESS

546.5 = 546

546.6 = 547

546.4 = 546



是否在SQL中

546.5 = 547

546.6 = 547

546.4 = 546



我想圆形函数,就像它在MS Access中如何工作

546.5 = 546

546.6 = 547

546.4 = 546


我尝试过:



select cast(round(2764.6,0) as int)

解决方案

如果您的数字只有一个小数位(即没有像546.51或546.49这样的数字),您可以使用:

 选择 cast(round(x-0。 01 ,<跨度lass =code-digit> 0 ) as   int  


查看MS Access文档:

Quote:



Microsoft Access Round 函数返回一个四舍五入到指定小数位数的数字。 然而, Round 函数表现得有点奇怪,并且使用通常被称为银行家四舍五入的东西。因此,在使用此函数之前,请阅读以下内容:



Round 函数使用舍入到偶数逻辑。 如果要舍入的表达式以5结尾,则 Round 函数将对表达式进行舍入,以使最后一位为偶数。





结论:你必须创建自定义函数,其行为与预期一致



来源:

MS Access:Round Function [ ^ ]

ROUND(Transact-SQL)| Microsoft Docs [ ^ ]


Hello All,

I am working on converting MS Access macros to sql,
While i converting i faced one serious issue.

Though ms access and sql are Microsoft product conversion of number works differently.
Ex.
IN MS ACCESS
546.5 =546
546.6=547
546.4=546

Whether in SQL
546.5 =547
546.6=547
546.4=546

I want to round function like how it works in MS Access
546.5 =546
546.6=547
546.4=546

What I have tried:

select cast(round(2764.6,0) as int)

解决方案

If your numbers only have one decimal position (that is there are no number like 546.51 or 546.49) you can use:

select cast(round(x-0.01,0) as int


Take a look at MS Access documentation:

Quote:


The Microsoft Access Round function returns a number rounded to a specified number of decimal places. However, the Round function behaves a little peculiar and uses something commonly referred to as bankers rounding. So before using this function, please read the following:

The Round function utilizes round-to-even logic. If the expression that you are rounding ends with a 5, the Round function will round the expression so that the last digit is an even number.



Conclusion: you have to create custom function which will behave as you expected

Source:
MS Access: Round Function[^]
ROUND (Transact-SQL) | Microsoft Docs[^]


这篇关于Sql round函数没有按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆