Mysql strip time component from datetime
No more partying ‘till late for your DATETIME
; strip time from DATETIME
using the DATE()
function:
This outputs only the date
as YYYY-MM-DD
, leaving the DATETIME
with nothing but a hangover (the time is discarded).
Don't Go Peeling Dates Just Yet...
Here's when you may not want to use DATE()
:
- Your data is time-sensitive.
- Precise timestamps are needed.
- The time zone cannot be ignored.
_[//]: # "//Dry humor: DATE() is like a time machine; it only goes one way – to the past."
More Than a One-Trick Pony: DATE_FORMAT()
DATE_FORMAT()
serves up dates in style (custom formats).
Smarter Date Comparisons
Just like in relationships, avoid being too clingy (no redundant function calls):
Be elegant and efficient – let MySQL do some of the work:
Time Zones: When Minutes Matter
Adjust date with respect to time zones:
We just converted datetime_col
to the system's time zone.
NULL Isn't Always Null and Void
When datetime_col
may contain NULL
:
You'll get a default date instead of a NullPointer ruining your day.
The Good, the Bad, and the (Sort of) Ugly: Legacy Data
Deal with dates stored in non-standard formats:
This is how you retrieve a date buried in a string column.
Was this article helpful?