sql server中的整数除法 [英] Integer division in sql server

查看:123
本文介绍了sql server中的整数除法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Microsoft SQL Server 2005 中,为什么以下命令会产生整数结果?

In Microsoft SQL Server 2005, why do the following commands produce integer results?

SELECT cast(151/6 AS DECIMAL(9,2))
SELECT 151/6

推荐答案

首先你得到两个整数的结果,然后将结果转换为 DECIMAL(9,2).在第二个中,您只是将两个整数相除,这是预期的.

In the first you are getting the result of two integers and then casting the result as DECIMAL(9,2). In the second you're just dividing two integers and that's expected.

如果你将其中一个整数转换为小数BEFORE你做除法,你会得到一个小数结果.

If you cast one of the integers as a decimal BEFORE you do the division, you'll get a decimal result.

SELECT 151/CAST(6 AS DECIMAL (9,2))

这篇关于sql server中的整数除法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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