How to select only date from a DATETIME field in MySQL?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
MySQL DATETIME columns contain both date and time, but many reports and filters need only the date part. Extracting a date value is easy in SELECT output, while filtering requires care to keep queries index-friendly. The best solution depends on whether you are formatting output, aggregating by day, or filtering large tables.
Select the Date Portion in Query Output
For projection only, use DATE(datetime_column).
This returns a date value, not text. It is usually the correct choice for API responses that represent day-level timestamps.
You can also use CAST:
Both are readable. Teams usually pick one style and keep it consistent.
DATE() Versus DATE_FORMAT()
Use DATE_FORMAT only when you need a specific string shape for presentation.
A formatted string is useful for exports, but avoid converting to text too early in data pipelines because text values are less convenient for date arithmetic and comparisons.
Group by Day for Aggregation
Daily metrics are a frequent requirement. Group by extracted day value:
This is easy to read and works well for moderate table sizes. For very large workloads, consider pre-aggregated tables or generated columns.
Filter by Date Without Killing Index Usage
A common mistake is filtering with WHERE DATE(created_at) = '2026-03-01'. Wrapping indexed columns in functions can prevent efficient index range scans.
Less efficient pattern:
Preferred pattern uses start-inclusive and end-exclusive boundaries:
This pattern typically preserves index benefits on created_at.
Time Zone Correctness
If data is stored in UTC but business reporting is local-time based, convert timezone before extracting date values. Otherwise rows near midnight can appear on the wrong reporting day.
Ensure MySQL timezone tables are loaded in environments where CONVERT_TZ is used.
Generated Date Column for Heavy Day Queries
If day-based filters are frequent and table volume is high, a generated column can simplify queries and indexing.
Then query directly:
This improves readability and can help planner choices depending on data distribution and workload.
Application Layer Parameter Handling
Pass explicit day boundaries from the application instead of concatenating SQL strings. This reduces SQL injection risk and timezone bugs.
Parameterized queries also keep query plans more stable.
Common Pitfalls
- Filtering with
DATE(column)in large tables and losing index efficiency. - Converting date values to strings too early, then doing fragile text comparisons.
- Ignoring timezone conversion for local business reports.
- Grouping by day without validating day boundaries for UTC-stored data.
- Hardcoding date literals in code instead of passing safe bound parameters.
Summary
- Use
DATE()orCAST(... AS DATE)to project date-only values. - Reserve
DATE_FORMATfor presentation text output. - Use range predicates on the raw
DATETIMEcolumn for performant filtering. - Convert timezone before date extraction when reporting is local-time based.
- Add generated date columns and indexes for frequent day-level analytics.

