The Oracle TRUNC function is a powerful tool that many database professionals underestimate. Have you ever wondered how to simplify complex data, such as dates and numbers, to create clearer and more accurate reports? Keep reading to discover how TRUNC can transform the way you handle data.
Previously, we discussed how to work with dates in this article, but this time we will focus on greater precision and on using the TRUNC function to make SQL queries more efficient.
What Is the TRUNC Function in Oracle?
The Oracle TRUNC function is used to truncate a number or date to a specific value, removing fractional parts or adjusting dates to a particular level of granularity. This is especially useful when you need to work with more manageable and standardized data.
Why Use TRUNC?
In the database world, especially in Oracle, precise and well-formatted data is crucial for decision-making. TRUNC lets you:
-
Truncate numbers without worrying about decimal parts.
-
Adjust dates to standardize reports and analyses.
Using TRUNC with Dates
Truncating dates can be extremely useful for grouping data by day, month, or year by removing the complexity of hours, minutes, and seconds.
Truncating to the Day
Suppose you have a date in the full DD-MON-YYYY HH:MI:SS format. Using TRUNC helps you simplify it to only the day portion.
SELECT TRUNC(SYSDATE) AS trunc_date FROM dual;
Output: 17-JUL-2024
Truncating to the Start of the Month
To group data by month, you can truncate the date to the first day of the month.
SELECT TRUNC(SYSDATE, 'MM') AS trunc_month FROM dual;
Output: 01-JUL-2024
Truncating to the Start of the Year
For yearly analysis, you can truncate the date to the beginning of the year.
SELECT TRUNC(SYSDATE, 'YYYY') AS trunc_year FROM dual;
Output: 01-JAN-2024
Using TRUNC with Numbers
Truncating numbers is useful for removing decimal parts and working with integer values or reducing a number to a specific number of decimal places without rounding.
Truncating a Number to an Integer
If you have a decimal number and want only its integer part:
SELECT TRUNC(123.456) AS trunc_number FROM dual;
Output: 123
Truncating a Number to Specific Decimal Places
You can truncate a number to a specific number of decimal places.
SELECT TRUNC(123.456, 2) AS trunc_number FROM dual;
Output: 123.45
Other Useful Uses of TRUNC
In addition to dates and numbers, TRUNC has other interesting uses in Oracle.
Truncating Dates by Hour
You can truncate dates to the beginning of each hour for more detailed analyses.
SELECT TRUNC(SYSDATE, 'HH') AS trunc_hour FROM dual;
Output: 17-JUL-2024 14:00:00
Truncating Dates to the Week
For grouping data by week:
SELECT TRUNC(SYSDATE, 'IW') AS trunc_week FROM dual;
Output: 15-JUL-2024
Conclusion
The Oracle TRUNC function is a versatile tool that can simplify and improve how you handle data. Whether you need to standardize dates or work with numbers more efficiently, TRUNC is a useful solution. Start using TRUNC in your queries and see how it improves the clarity and usefulness of your data.
Now that you know the basics and some advanced uses of TRUNC, why not start experimenting with your own data? Share your experiences and questions in the comments. Happy data analysis.

