MySQL SQL - 转换日期时间格式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10205873/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 13:01:01  来源:igfitidea点击:

SQL - Convert Datetime format

mysqldatetime-format

提问by user1324059

I try to change my format Datetime on MySQL

我尝试更改 MySQL 上的日期时间格式

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

T 得到了一个包含一些列的历史表,有些列都具有这种格式 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.

我没有 SS 的变量,我想取 00。

Example

例子

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

Thanks

谢谢

回答by Malhar Chudasama

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

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

回答by user1324059

use date_format()function

使用date_format()函数

Try below:

试试下面:

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

回答by FDADA

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

希望这可以帮助