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

前端 未结 3 655
陌清茗
陌清茗 2021-01-03 21:45

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 an

3条回答
  •  孤独总比滥情好
    2021-01-03 22:20

    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.

提交回复
热议问题