Skip to main content

truncate_value

1. TRUNCATE_VALUE Function

The TRUNCATE_VALUE function in H2 database is used to truncate a numeric value to a specified number of decimal places.

2. Syntax

The syntax for the TRUNCATE_VALUE function in H2 database is as follows:

TRUNCATE_VALUE(value, decimalPlaces)

Arguments

  • value: The numeric value that needs to be truncated.
  • decimalPlaces: The number of decimal places to which the value should be truncated. It should be an integer value.

Return

  • The TRUNCATE_VALUE function returns the truncated value.

3. Notes

  • The TRUNCATE_VALUE function in H2 database rounds the value towards zero to the specified number of decimal places.
  • If the input value is not a number or exceeds the numeric range, the TRUNCATE_VALUE function will return NULL.
  • Make sure to provide a valid integer value for the decimalPlaces argument. Providing a negative value will result in an error.

4. Examples

Here are a few examples demonstrating the usage of the TRUNCATE_VALUE function in H2 database:

Example 1 - Truncating a value to 2 decimal places:

SELECT TRUNCATE_VALUE(3.14159, 2) AS truncated_value;

Output:

truncated_value
---------------
3.14

Example 2 - Truncating values stored in a column:

CREATE TABLE prices (
id INT PRIMARY KEY,
price DECIMAL(10, 2)
);

INSERT INTO prices VALUES (1, 9.99), (2, 15.75), (3, 24.50);

SELECT id, price, TRUNCATE_VALUE(price, 1) AS truncated_price
FROM prices;

Output:

id | price | truncated_price
---+-------+----------------
1 | 9.99 | 9.9
2 | 15.75 | 15.7
3 | 24.50 | 24.5
  • round - Round a value to a specified number of decimal places.