oracle trunc

Explanation of Oracle's TRUNC Function in SQL

The TRUNC function in Oracle SQL is used to truncate a date value to a specified unit of measure. The syntax for the TRUNC function is as follows:

TRUNC(date, [format])
  • date: The date value to be truncated.
  • format: (Optional) The unit of measure to which the date should be truncated. If this parameter is omitted, the date is truncated to the nearest day.

The TRUNC function returns a date value truncated to the specified unit of measure.

Example Usage of TRUNC Function

Suppose we have a table orders with a column order_date containing date values. We want to retrieve all orders placed on a specific day, ignoring the time component of the order_date column.

We can use the TRUNC function to achieve this as follows:

SELECT * FROM orders WHERE TRUNC(order_date) = TRUNC(SYSDATE);

In this example, TRUNC(order_date) truncates the order_date to the nearest day, and TRUNC(SYSDATE) truncates the current date to the nearest day. By comparing these truncated values, we can retrieve all orders placed on the current day, regardless of the time component in the order_date column.

Conclusion

The TRUNC function in Oracle SQL is a powerful tool for manipulating date values by truncating them to a specified unit of measure. It is commonly used to perform date-based comparisons and calculations in SQL queries.