Create a computed column and round

风格不统一 提交于 2020-01-30 06:34:27

问题


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.

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.

My new column definition is ((Length * Width * Height) / 166)

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


回答1:


You need to convert the computed column to numeric(18,2)

Example :

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 


来源:https://stackoverflow.com/questions/28174372/create-a-computed-column-and-round

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!