SQL中用于划分结果的十进制值 [英] Decimal values in SQL for dividing results

查看:14
本文介绍了SQL中用于划分结果的十进制值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 SQL 中,我有 col1col2.两者都是整数.

In SQL, I have col1 and col2. Both are integers.

我想做:

select col1/col2 from tbl1

我得到结果 1 其中 col1=3col2=2

I get the result 1 where col1=3 and col2=2

我想要的结果是1.1

我把round(col1/col2,2).结果还是1.

我输入了decimal(col1/col2,2).小数点不是内置函数.

I put decimal(col1/col2,2). The decimal is not built in function.

我该怎么做才能获得 1.1?

How can I do exactly to get 1.1?

推荐答案

在除法之前,您需要将值强制转换或转换为十进制.看看这个http://msdn.microsoft.com/en-us/library/aa226054.aspx

You will need to cast or convert the values to decimal before division. Take a look at this http://msdn.microsoft.com/en-us/library/aa226054.aspx

例如

DECLARE @num1 int = 3 DECLARE @num2 int = 2

SELECT @num1/@num2

SELECT @num1/CONVERT(decimal(4,2), @num2)

第一个 SELECT 将产生您所看到的结果,而第二个 SELECT 将有正确的答案 1.500000

The first SELECT will result in what you're seeing while the second SELECT will have the correct answer 1.500000

这篇关于SQL中用于划分结果的十进制值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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