114

I need to do a date comparison in Mysql without taking into account the time component i.e. i need to convert '2008-11-05 14:30:00' to '2008-11-05'

Currently i am doing this:

SELECT from_days(to_days(my_date))

Is there a proper way of doing this?

5 Answers 5

201

Yes, use the date function:

SELECT date(my_date)
1
  • This is the one. Thanks. Last on the list of Answers for some reason. Dec 9, 2021 at 16:43
25

select date(somedate) is the most common.

If you need to accommodate other formats, you can use:

SELECT DATE_FORMAT(your_date, '%Y-%m-%d');
1
  • 2
    The DATE_FORMAT is very useful for extracting the year component only for example. Thank you.
    – Mario Awad
    Sep 20, 2012 at 9:21
3

In PostgreSQL you use the TRUNC() function, but I'm not seeing it for MySQL. From my brief Googling, it looks like you'll need to cast your DATETIME value to be just DATE.

date_col = CAST(NOW() AS DATE)

See http://dev.mysql.com/doc/refman/5.0/en/date-and-time-types.html

1
  • 1
    date_trunc() (not trunc()) wouldn't actually remove the time part in Postgres. It would merely set it to 00:00:00 which is something different.
    – user330315
    May 6, 2013 at 21:01
-1

Just a simple way of doing it date("d F Y",strtotime($row['date'])) where $row['date'] comes from your query

-5

You could use ToShortDateString();

2
  • Isn't that a C# function? The question is after a MySQL solution
    – andrewsi
    Sep 25, 2012 at 21:12
  • It's C# and actually it does not remove the timestamp: Converts the value of the current DateTime object to its equivalent short date string representation.
    – Nizar B.
    Dec 4, 2014 at 14:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.