Truncating Dates in Oracle: Group By Minute Instead of Per Day Using TRUNC Function

Truncating Dates in Oracle: Group By Minute Instead of Per Day

When working with dates and times in Oracle, it’s common to need to perform calculations or group data by specific intervals. In this article, we’ll explore how to achieve a group by minute instead of per day using the TRUNC function.

Understanding the Problem

The original query aims to retrieve data received per day:

alter session set nls_date_format='yyyy/mm/dd hh24:mi:ss';
SELECT 
  to_char(created_date, 'yyyy/mm/dd'), 
  status_code, 
  COUNT(workflow_txn_id_log)
FROM 
  workflow_txn_log
WHERE 
  status_code = 'DOWNLOAD_ALL' AND created_date > '2021/08/11'
GROUP BY 
  to_char(created_date, 'yyyy/mm/dd'), 
  status_code
ORDER BY 
  to_char(created_date, 'yyyy/mm/dd');

However, the requirement changes to group by minute instead of per day. This involves truncating the date to the minute and grouping the data accordingly.

Truncating Dates in Oracle

The TRUNC function in Oracle is used to truncate a date or time value to a specific interval (e.g., day, month, year). When used with DATE, it returns a new date that has the same date component but truncated to the specified interval’s components.

In this case, we want to truncate the created_date column to the minute. The syntax for TRUNC is as follows:

TRUNC(date_value, 'interval')

Where date_value is the input date or time value, and 'interval' specifies the interval to which we want to truncate the value.

For minutes, the format string is 'mi'. This means that any fractional seconds associated with the original created_date value will be truncated.

Changing Date Format in Oracle

Before applying TRUNC, it’s often necessary to change the date format to facilitate easier comparison and grouping. In this case, we need to switch from the default format yyyy/mm/dd hh24:mi:ss to a format that only includes the minute component.

By setting nls_date_format to 'yyyy/mm/dd', we ensure that any subsequent date comparisons are performed in day-based order instead of the more granular minute-based order. However, this approach has its limitations and may lead to implicit conversions when used with certain functions like TRUNC.

Solving the Problem

Given our new requirements, let’s modify the original query to use TRUNC to truncate the created_date column to minutes:

SELECT 
  trunc(created_date, 'mi') created_minute,
  status_code,
  COUNT(workflow_txn_id_log)
FROM 
  workflow_txn_log
WHERE 
  status_code = 'DOWNLOAD_ALL' AND created_date > date '2021-08-11'
GROUP BY 
  trunc(created_date, 'mi'), 
  status_code
ORDER BY 
  trunc(created_date, 'mi');

In this revised query:

  • trunc(created_date, 'mi') trims the original created_date value to its minute component.
  • The comparison for the WHERE clause now uses a hardcoded date literal (date '2021-08-11') instead of a relative date format, ensuring accurate matching without relying on string conversions.

Explanation and Implications

The key takeaway here is that when working with dates in Oracle, it’s essential to consider the implications of implicit or explicit conversions. By using TRUNC and explicitly specifying the interval for truncation, we can avoid such issues and achieve more precise results.

Moreover, understanding how date formats impact comparisons and grouping operations helps developers design more effective queries that cater to diverse data needs.

Conclusion

In this article, we explored an Oracle query scenario where the original requirement was to group by day but changed to minute-level granularity. We demonstrated how to use the TRUNC function to truncate dates in Oracle to achieve the desired result while minimizing implicit conversions and ensuring accurate date comparisons.


Last modified on 2025-01-31