SQL - Convert Datetime format

拥有回忆 提交于 2019-12-06 05:15:06

问题


I try to change my format Datetime on MySQL

T got a table historic with some columns and some all them have this format DATETIME :

JJ/MM/AAAA HH:MM

I need to change to :

AAAA-MM-JJ HH:MM:SS

I don't have the variable for SS I would like to take 00.

Example

01/12/2012 12:23 > 2012-12-01 12:23:00

Thanks


回答1:


You need to use Format Function for that

For above you can use

SELECT DATE_FORMAT([YOURCOLUMNNAME],'%Y-%m-%d %h:%i:%s') from [YOURTABLENAME];

For all the conversion you can refer to.

http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format




回答2:


use date_format() function

Try below:

  SELECT  date_format(columnname,'%Y-%m-%d %h:%i:%s') as yourdate FROM tablename



回答3:


None of the posted answers will work, as your data won't be recognized as a date. Use string conversion instead:

UPDATE tablename SET columnname = CONCAT(SUBSTR(columnname,7,4),'-',SUBSTR(columnname,4,2),'-',SUBSTR(columnname,1,2),SUBSTR(columname,11),':00');

Hope this helps



来源:https://stackoverflow.com/questions/10205873/sql-convert-datetime-format

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!