Lesson

MySQL DATE() Function

10 min reading
Free Course

MySQL DATE(): Extract the Calendar Date From a Timestamp

The DATE() function extracts the 'YYYY-MM-DD' date part from a DATE, DATETIME, or valid date-time string expression. It discards hours, minutes, seconds, and microseconds cleanly.

Reporting queries rely on DATE() to group timestamped transactions by calendar day and display clean dates without clock clutter.

Syntax

Pass a single date or datetime expression to DATE():

DATE(expr)
  • expr: A DATE, DATETIME, TIMESTAMP, or string formatted like '2026-05-12 14:30:00'.

The function returns a DATE value, or NULL if the input cannot be parsed as a valid calendar date.

Practical Example

Suppose your analytics team aggregates daily revenue from demo_orders, where created_at stores full DATETIME timestamps.

SELECT
    DATE(created_at) AS order_day,
    COUNT(*) AS total_orders,
    SUM(total_amount) AS daily_revenue
FROM demo_orders
GROUP BY DATE(created_at)
ORDER BY order_day;
order_day total_orders daily_revenue
2026-05-10 4 620.00
2026-05-11 7 1145.50
2026-05-12 3 412.25

Grouping by DATE(created_at) merges orders placed at 08:15:00 and 21:40:00 on the same day into a single summary row.

Common Mistakes & NULL Behavior

Keep these performance and correctness points in mind:

  • NULL or invalid dates return NULL: Calling DATE(NULL) or DATE('2026-02-30') returns NULL rather than raising an error during SELECT.
  • Wrapping indexed columns slows WHERE filters: Filtering with WHERE DATE(created_at) = '2026-05-12' forces a full table scan because MySQL cannot use a standard index on created_at. Write WHERE created_at >= '2026-05-12 00:00:00' AND created_at < '2026-05-13 00:00:00' instead.
  • No TIME-only input: Passing a standalone time like DATE('14:30:00') has no calendar date and returns NULL.
  • String formatting expectations: DATE() always outputs ISO YYYY-MM-DD. If you need 'May 12, 2026', wrap the result in DATE_FORMAT().

Frequently Asked Questions

Does DATE() round up if the time is 23:59:59?

No. DATE() simply truncates the time portion, so '2026-05-12 23:59:59' always returns '2026-05-12'.

Can I use DATE() on a TIMESTAMP column?

Yes. MySQL first converts the TIMESTAMP value to the current session time zone, then extracts the date part.

What happens when I pass a numeric date like 20260512 to DATE()?

MySQL parses valid 8-digit YYYYMMDD integers and returns '2026-05-12', while invalid numbers like 20261301 return NULL.

Save Your Progress

Unlock Your
Full Potential.

Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.

Quick Access With

Enterprise-Grade Security Protocol

Try it Yourself

Experiment with the code from this lesson in our interactive playground.

Open Playground
MySQL Topic Hub

Stuck on this lesson?

Join our community of senior developers.

Ask in Forum