SQL 在teradata中将十进制转换为整数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21333976/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
converting decimal to integer in teradata
提问by user3055262
sel cast(9.1 as integer)as inttt;
Result:
9
sel cast(9.9 as integer)as inttt;
Result:
9
I executed the above queries on Teradata, and both of the queries resulted in the floor value.
我在 Teradata 上执行了上述查询,这两个查询都得到了下限值。
Is this a hard rule for casting from DEC to INT? That is, does it always return the floor value?
这是从 DEC 转换为 INT 的硬性规则吗?也就是说,它是否总是返回下限值?
回答by juergen d
Yes it is.
是的。
If you cast a decimal value into an integer the decimal places get lost. That is in all languages (I know of) the case.
如果将十进制值转换为整数,小数位会丢失。在所有语言(我知道)中都是如此。
回答by GP-by
Following code can be used to round decimal number:
以下代码可用于舍入十进制数:
Select CAST(number + 0.5 AS integer) from sometable
回答by dnoeth
As juergen_d already said, this should be the default for any language.
正如 juergen_d 已经说过的,这应该是任何语言的默认设置。
When you want rounding istead of truncating, you do a CAST(9.9 AS DEC(9,0))or in TD14 there's a ROUNDfunction, too. But be aware, there are two algorithms for rounding, check if your system uses the one you want :-)
当您想要舍入而不是截断时,您可以执行CAST(9.9 AS DEC(9,0))或在 TD14 中也有一个ROUND函数。但请注意,有两种舍入算法,请检查您的系统是否使用您想要的算法:-)
回答by evanv
Which version of TD are you using? If 14, just use the CEIL function?
你用的是哪个版本的TD?14、如果只是使用CEIL函数?
If before that, maybe try
如果在那之前,也许试试
CASE WHEN CAST(number AS DEC(9,0) < number THEN CAST(number AS
DEC(9,0) + 1 ELSE CAST(number AS DEC(9,0)
(you could obviously adjust that to your own rounding logic)
(您显然可以将其调整为您自己的舍入逻辑)
回答by Spon4ik
try this
尝试这个
SELECT CAST([COLUMN] AS INT) + CASE WHEN [COLUMN] MOD 1 >= 0.5 THEN 1 ELSE 0 END from [table]
results:
9.1 9
9.2 9
9.3 9
9.4 9
9.5 10
9.6 10
9.7 10
9.8 10
9.9 10
10 10