Perform numerical truncation on x to the number of decimal places d
TRUNCATE(<x>, <d>)
| Parameter | Description |
|---|---|
<x> | The value that needs to be numerically truncated |
<d> | The number of decimal places to retain |
Perform numerical truncation on x to the number of decimal places d. Truncation rules:
If d is a literal:
If d is a column, and the first argument is of type Decimal, then the resulting Decimal will have the same number of decimal places as the input Decimal
d is a literal
select truncate(124.3867, 2),truncate(124.3867, 0),truncate(124.3867, -2);
+-----------------------+-----------------------+------------------------+ | truncate(124.3867, 2) | truncate(124.3867, 0) | truncate(124.3867, -2) | +-----------------------+-----------------------+------------------------+ | 124.38 | 124 | 100 | +-----------------------+-----------------------+------------------------+
d is a column
select cast("123.123456" as Decimal(9,6)), number, truncate(cast ("123.123456" as Decimal(9,6)), number) from numbers("number"="5");
+---------------------------------------+--------+----------------------------------------------------------------------+ | cast('123.123456' as DECIMALV3(9, 6)) | number | truncate(cast('123.123456' as DECIMALV3(9, 6)), cast(number as INT)) | +---------------------------------------+--------+----------------------------------------------------------------------+ | 123.123456 | 0 | 123.000000 | | 123.123456 | 1 | 123.100000 | | 123.123456 | 2 | 123.120000 | | 123.123456 | 3 | 123.123000 | | 123.123456 | 4 | 123.123400 | +---------------------------------------+--------+----------------------------------------------------------------------+