创建一个计算列并四舍五入 [英] Create a computed column and round

查看:62
本文介绍了创建一个计算列并四舍五入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的表格中有3个数字(18,2)列。我想创建一个第四列,它是计算出的。与其他3列一样,我在SSMS中将计算列创建为数字(18,2)

I have 3 numeric(18,2) columns in my table. I want to create a fourth column that is computed. I created the computed column in SSMS as numeric (18,2) just like the other 3 columns.

当我对新列运行 SELECT 时,它可以很好地进行计算,但是不会四舍五入到小数点后两位的地方。我尝试在计算的列定义中使用 ROUND 函数,但是没有用。它将我的数据类型重置为数字(38,6),现在不允许我更改它。

When I run a SELECT against my new column, it computes it fine, but it's not rounded to two decimal places. I've tried using the ROUND function inside my computed column definition but that didn't work. It reset my data type to numeric (38,6) and now won't let me change it.

我的新列定义是(((长度*宽度*高度)/ 166)

我希望最终结果四舍五入小数点后两位。我在做什么错?

I want the end result rounded to two decimal places. What am I doing wrong?

推荐答案

您需要转换计算列数字(18,2)

示例:

CREATE TABLE computed
  (
     a NUMERIC(18, 2),
     b NUMERIC(18, 2),
     c AS CONVERT(NUMERIC(18, 2), a * b)
  )

INSERT INTO computed VALUES (1,2)

SELECT *
FROM   computed 

这篇关于创建一个计算列并四舍五入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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