Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I convert datetime to date, truncating the times, leaving me the dates?

Tags:

sql

mysql

I have a field that's in datetime format when date would be better and more consistent with the rest of the database, so I want to convert. The time part is all 00:00:00 anyway.

How can I do this in MySQL?

Thanks.

like image 287
i-CONICA Avatar asked Sep 23 '11 10:09

i-CONICA


3 Answers

If you want this in a SELECT-Statement, just use the DATE Operator:

SELECT DATE(`yourfield`) FROM `yourtable`;

If you want to change the table structurally, just change the datatype to DATE (of course only do this if this doesn't affect applications depending on this field).

ALTER TABLE `yourtable` CHANGE `yourfield` `yourfield` DATE;

Both will eliminate the time part.

like image 122
Bjoern Avatar answered Oct 29 '22 17:10

Bjoern


Cast it as a DATE:

select DATE(my_date_time)

That will truncate the time from it, leaving only the date part.

like image 31
Dave Avatar answered Oct 29 '22 17:10

Dave


when using change we have to repeat the same name again for the field.now we can use MODIFY to alter the filed type.

ALTER TABLE `yourtable` MODIFY `yourfield` DATE;

like image 37
zabusa Avatar answered Oct 29 '22 16:10

zabusa