0

列值 = 1234.24 SQL 查询:

Cast(Round(Column_Name, 2, 1) AS Decimal (18,2)) As New_Column

给我 Value as 1234.23,我知道它会将我的最后一位数字截断 1 到 2 位小数。雪花查询:

Cast(Round(Column_Name),2) AS Decimal (18,2) As New_Column

给我1234.24因为我们只能使用 2 个值 "Cast(Round(Column_Name) ,1.6 ) AS Decimal (18,2) As New_Column" 给我1234.23作为预期结果,

请解释一下SQL中的(,2,1)和雪花中的(,1.6)的关系是什么?我们可以使用十进制值吗?有没有什么选项可以像我们在 SQL 中那样在雪花中传递 3 个值?

4

1 回答 1

0

For Snowflake:

ROUND accepts arguments, and the second one is "scale expression" which indicated the number of digits the output should include after the decimal point. It should evaluate to an integer from -38 to +38. So you should not enter 1.6. Although it works now, someday it may be fixed and produce an error.

https://docs.snowflake.com/en/sql-reference/functions/round.html

For Microsoft SQL Server:

The third parameter indicated the function: When the function is omitted or has a value of 0 (default), numeric_expression is rounded. When a value other than 0 is specified, numeric_expression is truncated.

https://docs.microsoft.com/en-us/sql/t-sql/functions/round-transact-sql?view=sql-server-ver15

So when you use, "Round(Column_Name, 2, 1)", you truncate the rest of the digits instead of rounding the number. If you want to do the same thing, you can use the TRUNCATE function in Snowflake:

https://docs.snowflake.com/en/sql-reference/functions/trunc.html

I suspect that you store your value as a floating-point number. Floating point numbers are approximate values. A floating point number might not round as expected.

https://docs.snowflake.com/en/sql-reference/data-types-numeric.html#float-float4-float8

于 2021-09-24T07:55:55.360 回答