Redshift 将十进制转换为十进制
Redshift casting decimal to decimal
为什么将小数转换为小数会丢失所有小数?例如,
SELECT 1.5::DECIMAL
Returns 2
十进制数有精度(整数中有效位数的总和)和小数位(小数位数)。
在 Redshift 中,默认精度为 18
, 但 默认比例为 0
,并且在转换时应用自动舍入,如 explained in the documentation:
scale
The number of decimal digits in the fractional part of the value, to the right of the decimal point. [...] The default scale, if not specified, is 0.
[...]
If the scale of an input value that is loaded into a table is greater than the scale of the column, the value is rounded to the specified scale.
因此您需要在投射时指定比例,例如:
SELECT CAST(1.5 AS DECIMAL(10, 5))
为什么将小数转换为小数会丢失所有小数?例如,
SELECT 1.5::DECIMAL
Returns 2
十进制数有精度(整数中有效位数的总和)和小数位(小数位数)。
在 Redshift 中,默认精度为 18
, 但 默认比例为 0
,并且在转换时应用自动舍入,如 explained in the documentation:
scale
The number of decimal digits in the fractional part of the value, to the right of the decimal point. [...] The default scale, if not specified, is 0.
[...]
If the scale of an input value that is loaded into a table is greater than the scale of the column, the value is rounded to the specified scale.
因此您需要在投射时指定比例,例如:
SELECT CAST(1.5 AS DECIMAL(10, 5))