How to Round Timestamps in Snowflake

Snowflake provides powerful functionality for manipulating timestamps in SQL queries. Rounding timestamps can be particularly useful when you need to standardize time data or analyze it in fixed intervals. In this tutorial, we will explore how to round timestamps to various intervals such as hours, minutes, and seconds.

1. Why Round Timestamps?

Rounding timestamps is an important operation in data analysis, especially when you're working with time-series data or need to group data by specific time intervals. For instance, rounding timestamps to the nearest hour or minute helps consolidate data for reporting, aggregation, or comparison purposes.

2. Snowflake Timestamp Functions

In Snowflake, you can round timestamps using various date and time functions. The most commonly used function for rounding is DATE_TRUNC. Let's see how it works:


SELECT DATE_TRUNC('HOUR', CURRENT_TIMESTAMP()) AS rounded_to_hour;
SELECT DATE_TRUNC('MINUTE', CURRENT_TIMESTAMP()) AS rounded_to_minute;
SELECT DATE_TRUNC('SECOND', CURRENT_TIMESTAMP()) AS rounded_to_second;
                                

The DATE_TRUNC function allows you to round the timestamp to a specified part of the date/time, such as:

  • SECOND - rounds to the nearest second
  • MINUTE - rounds to the nearest minute
  • HOUR - rounds to the nearest hour
  • DAY - rounds to the nearest day
  • MONTH - rounds to the nearest month
  • YEAR - rounds to the nearest year

3. Example: Rounding to Different Intervals

Let's consider a timestamp of 2025-05-07 14:37:45. Below are some examples of how this timestamp can be rounded using Snowflake's DATE_TRUNC:


-- Round to nearest hour
SELECT DATE_TRUNC('HOUR', '2025-05-07 14:37:45') AS rounded_hour;
-- Result: 2025-05-07 14:00:00

-- Round to nearest minute
SELECT DATE_TRUNC('MINUTE', '2025-05-07 14:37:45') AS rounded_minute;
-- Result: 2025-05-07 14:37:00

-- Round to nearest second
SELECT DATE_TRUNC('SECOND', '2025-05-07 14:37:45') AS rounded_second;
-- Result: 2025-05-07 14:37:45
                                

4. Additional Considerations

When rounding timestamps, it's important to be aware of time zone considerations, as Snowflake allows you to work with timestamps in different time zones. You may need to adjust your time zone settings to ensure the rounding is done in the correct time zone.

5. Conclusion

Rounding timestamps in Snowflake is a simple yet powerful way to normalize time-based data. By using the DATE_TRUNC function, you can round your timestamps to any desired interval, making it easier to group and analyze your data. This tutorial has shown how to round timestamps to the nearest hour, minute, and second. Experiment with these functions to see how they fit into your data workflows.