Skip to content

Overview

Truncating dates in MySQL is a process used to normalize date values by removing the time component. This guide explains how to truncate dates, simplifying date-related queries and comparisons.

Understanding date truncation

Date truncation involves reducing a date-time value to just its date component. It’s commonly used in scenarios where time precision isn’t needed.

Method: DATE() Function

Use the DATE() function to truncate a date-time value. This function extracts the date part from a date-time expression.

SELECT DATE(datetime_column) FROM your_table;

Method: DATE_FORMAT() Function

For more flexibility, DATE_FORMAT() allows formatting dates. To truncate, format the date-time as a date.

SELECT DATE_FORMAT(datetime_column, '%Y-%m-%d') FROM your_table;

Truncating dates for comparison

Comparing dates becomes easier after truncation. Use DATE() in your WHERE clause for date-only comparisons.

SELECT * FROM your_table WHERE DATE(datetime_column) = '2023-01-01';

Use in aggregate functions

Truncating dates is useful in aggregate functions like GROUP BY. It allows grouping by day, regardless of time.

SELECT DATE(datetime_column), COUNT(*) FROM your_table GROUP BY DATE(datetime_column);

Handling different time zones

When working with different time zones, convert the time zone before truncating to ensure accuracy.

SELECT DATE(CONVERT_TZ(datetime_column, '+00:00', 'Your_Time_Zone')) FROM your_table;

Performance considerations

  • Index Usage: Truncating dates in a query can prevent the use of indexes. Consider the impact on performance.
  • Precomputed Columns: If date truncation is common, consider storing the truncated date in a separate column.

Advanced scenarios

Combining with other functions

Truncate dates and then use other functions for complex queries.

SELECT MIN(DATE(datetime_column)) FROM your_table WHERE your_conditions;

Handling NULL values

Ensure your queries handle NULL values appropriately when truncating dates.

SELECT DATE(IFNULL(datetime_column, NOW())) FROM your_table;

Conclusion

Date truncation in MySQL simplifies date handling, aiding in clearer, more efficient data querying and analysis. Remember to consider the performance implications and handle special cases like time zones and NULL values.

Basedash is built as an AI-native BI platform, so teams can go from ad hoc SQL to trusted answers and dashboards quickly, without the overhead of traditional BI setup.

Written by

Robert Cooper avatar

Robert Cooper

Senior Engineer at Basedash

Robert Cooper is a senior engineer at Basedash who builds full-stack product systems across SQL data infrastructure, APIs, and frontend architecture. His work focuses on application performance, developer velocity, and reliable self-hosted workflows that make data operations easier for teams at scale.

View full author profile →

Looking for an AI-native BI tool?

Basedash lets you build charts, dashboards, and reports in seconds using all your data.