Worldscope

TRUNC()

Palavras-chave:

Publicado em: 04/08/2025

Understanding and Using TRUNC() in Oracle SQL

The TRUNC() function in Oracle SQL is used to truncate numbers or dates to a specified unit. This article provides a comprehensive guide to understanding and utilizing TRUNC(), including its syntax, usage examples, and alternative approaches.

Fundamental Concepts / Prerequisites

Before diving into TRUNC(), it's helpful to have a basic understanding of:

  • SQL syntax
  • Data types in Oracle SQL (specifically numbers and dates)
  • The concept of truncation versus rounding

TRUNC() Implementation in Oracle SQL

The TRUNC() function can be used with both numbers and dates. Here are examples illustrating its usage in Oracle SQL.


-- Truncating a number:
SELECT TRUNC(123.456) AS truncated_number_default FROM dual;  -- Returns 123 (truncates to the nearest integer)

SELECT TRUNC(123.456, 2) AS truncated_number_2_decimal_places FROM dual; -- Returns 123.45 (truncates to two decimal places)

SELECT TRUNC(123.456, -2) AS truncated_number_negative_2 FROM dual; -- Returns 100 (truncates to the nearest hundred)

-- Truncating a date:
SELECT TRUNC(TO_DATE('2023-10-27', 'YYYY-MM-DD')) AS truncated_date_default FROM dual; -- Returns 2023-10-27 00:00:00 (truncates to midnight)

SELECT TRUNC(TO_DATE('2023-10-27', 'YYYY-MM-DD'), 'MM') AS truncated_date_to_month FROM dual; -- Returns 2023-10-01 00:00:00 (truncates to the first day of the month)

SELECT TRUNC(TO_DATE('2023-10-27', 'YYYY-MM-DD'), 'YEAR') AS truncated_date_to_year FROM dual; -- Returns 2023-01-01 00:00:00 (truncates to the first day of the year)

Code Explanation

Number Truncation:

  • TRUNC(123.456): Without specifying the second argument (number of decimal places), it truncates the number to the nearest integer.
  • TRUNC(123.456, 2): The second argument, 2, specifies the number of decimal places to keep. It truncates the number, removing everything after the second decimal place.
  • TRUNC(123.456, -2): A negative second argument truncates digits to the left of the decimal point. In this case, it truncates to the nearest hundred.

Date Truncation:

  • TRUNC(TO_DATE('2023-10-27', 'YYYY-MM-DD')): When used with dates without a format specifier, it truncates the date to midnight (00:00:00).
  • TRUNC(TO_DATE('2023-10-27', 'YYYY-MM-DD'), 'MM'): Truncates the date to the first day of the month ('MM' specifies the month).
  • TRUNC(TO_DATE('2023-10-27', 'YYYY-MM-DD'), 'YEAR'): Truncates the date to the first day of the year ('YEAR' specifies the year).

Complexity Analysis

The TRUNC() function in Oracle SQL has a time complexity of O(1). It performs a direct calculation based on the input value and the specified truncation level. It doesn't involve iterating through a collection of data, so its execution time remains constant regardless of the input value. The space complexity is also O(1), as it only uses a constant amount of memory to store the result of the truncation operation.

Alternative Approaches

While TRUNC() is the most direct and efficient way to truncate numbers and dates, you could simulate number truncation using other functions. For example, you could use the FLOOR() function in combination with multiplication and division to achieve a similar result, but it's significantly less efficient and readable.


-- Simulating TRUNC(123.456, 2) using FLOOR():
SELECT FLOOR(123.456 * 100) / 100 FROM dual;

This approach, while technically working, is less clear and performant compared to using TRUNC() directly. For dates, there are no real alternative approaches without custom pl/sql functions.

Conclusion

The TRUNC() function is a powerful and efficient tool in Oracle SQL for truncating numbers and dates. Understanding its syntax and how to use it with different arguments is crucial for data manipulation and reporting. Compared to alternative methods, TRUNC() offers superior performance and readability for truncation operations.