MySQL: Get the datetime field with time part reset to midnight (00:00:00)?

I am trying to query the PurchaseDate datetime column from tmp table along with a custom column that is PurchaseDate column with time as 00:00:00 / midnight:

PurchaseDateStartOfDay
1996-07-16 20:00:001996-07-16 00:00:00
1996-07-10 21:19:001996-07-10 00:00:00
1996-07-12 22:18:001996-07-12 00:00:00

I wasn’t able to do it using DATEDIFF(). How can I do it in the simplest way?

As suggested, I have tried using date(). However that converts the column type to date and I would like to retain it as a datetime.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Convert to date to truncate the time part, then cast to datetime:

select
    PurchaseDate,
    cast(date(PurchaseDate) as datetime) as StartOfDay
from mytable

See live demo.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x