Why does casting a decimal to decimal lose all decimals? For example,
SELECT 1.5::DECIMAL
Returns 2
Advertisement
Answer
A decimal number has a precision (the total number of significant digits in the whole number) and a scale (the number of decimal digits).
In Redshift, the default precision is 18
, but the default scale is 0
, and automatic rounding is applied when casting, as 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.
So you would need to specify a scale when casting, for example:
SELECT CAST(1.5 AS DECIMAL(10, 5))