In many data processing scenarios, precision matters more than approximation.
While functions like ROUND perform rounding, sometimes we need strict numeric truncationβcutting off digits without any rounding logic.
In GBase 8s database, the TRUNCATE function is designed exactly for this purpose.
π 1. What is TRUNCATE in GBase 8s?
The TRUNCATE function performs exact numeric truncation, meaning:
- It cuts off digits beyond a specified precision
- It does NOT apply rounding
- It ensures deterministic numeric output
π This makes it ideal for:
- Financial calculations
- Scientific data processing
- Reporting systems
- Precision-sensitive analytics
βοΈ 2. Syntax of TRUNCATE
TRUNCATE(n [, m])
`
Parameters:
- n β The numeric value to be truncated
- m β Number of digits to keep
Behavior of m:
| Value of m | Meaning |
|---|---|
| m > 0 | Truncate to m decimal places |
| m = 0 | Truncate to integer |
| m < 0 | Truncate to left of decimal point |
π 3. Basic Example
Truncate to 2 decimal places
sql id="gbase_truncate_01"
SELECT TRUNCATE(1234.234, 2) FROM dual;
Result:
plaintext
1234.23
π Notice: No rounding occurs (unlike ROUND function)
π’ 4. Integer-Level Truncation
Example: Truncate to integer
sql id="gbase_truncate_02"
SELECT TRUNCATE(1234.234, 0) FROM dual;
Result:
plaintext
1234
π 5. Left-Side Truncation (Negative m)
Example:
sql id="gbase_truncate_03"
SELECT TRUNCATE(1234.234, -2) FROM dual;
Result:
plaintext
1200
π Removes digits to the left of the decimal point
β οΈ 6. Special Cases
NULL Input
sql
SELECT TRUNCATE(NULL, 2) FROM dual;
π Returns NULL
Empty String
sql
SELECT TRUNCATE('', 2) FROM dual;
π Returns NULL
Overflow Decimal Precision
If m exceeds available decimal places:
- Result is padded with zeros
Example:
sql id="gbase_truncate_04"
SELECT TRUNCATE(12.3, 4) FROM dual;
Result:
plaintext
12.3000
π§ 7. TRUNCATE vs ROUND
| Function | Behavior |
|---|---|
| ROUND | Rounds values |
| TRUNCATE | Cuts off values |
Example Comparison:
sql id="gbase_compare_01"
SELECT ROUND(1234.235, 2), TRUNCATE(1234.235, 2) FROM dual;
Result:
plaintext
ROUND β 1234.24
TRUNCATE β 1234.23
β‘ 8. Real-World Use Cases
β Financial Systems
- Prevent rounding errors in accounting
- Ensure strict monetary precision
β Scientific Calculations
- Maintain controlled precision levels
- Avoid propagation of rounding errors
β Reporting Systems
- Standardize numeric display formats
- Ensure consistent output across reports
π§© 9. Why TRUNCATE Matters in Databases
In real systems:
- Rounding can introduce bias
- Small precision errors accumulate
- Deterministic results are critical
π TRUNCATE ensures predictable numeric behavior
π Final Thoughts
The TRUNCATE function in GBase 8s database is a simple but powerful tool for:
- Precision control
- Deterministic numeric processing
- Clean data output formatting
Unlike rounding functions, it provides strict numeric control without approximation.
Top comments (0)