如何修复截断的错误日期时间值:'插入时'2017年4月25日上午11:48:45'错误?

时间:2017-06-01 07:00:36

标签: mysql sql datetime

我的日期格式为:Apr 25, 2017 11:48:45 AM我已使用STR_TO_DATE('Apr 25, 2017 11:48:45 AM', '%b %d, %Y %h:%i:%s')将其转换为日期时间格式。现在,我的表的形状包含time作为格式化的日期:

enter image description here

我想将此表插入另一个表中:

表格

CREATE TABLE `locations` (
  `uid` int(11) NOT NULL,
  `latitude` varchar(255) DEFAULT NULL,
  `longitude` varchar(255) DEFAULT NULL,
  `time` datetime NOT NULL,
  `created_at` datetime NOT NULL,
  KEY `locations_uid_IDX` (`uid`,`time`) USING BTREE,
  KEY `locations_time_IDX` (`time`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8

INSERTION:

INSERT INTO Transformation_Tables.locations

SELECT 
    uid, 
    latitude, 
    longitude, 
    STR_TO_DATE(time, '%b %d, %Y %h:%i:%s') as time,
    STR_TO_DATE(created_at, '%Y-%m-%d %H:%i:%s') as created_at
from creditfix.locations;

在插入过程中,我遇到了这个错误:

SQL Error [1292] [22001]: Data truncation: Truncated incorrect datetime value: 'Apr 25, 2017 11:48:45 AM'
  com.mysql.jdbc.MysqlDataTruncation: Data truncation: Truncated incorrect datetime value: 'Apr 25, 2017 11:48:45 AM' . Please help me out.

1 个答案:

答案 0 :(得分:0)

ISSUE

 STR_TO_DATE(time, '%b %d, %Y %h:%i:%s') as time

溶液

STR_TO_DATE('May 12, 2017 3:05:19 PM', '%b %d, %Y %l:%i:%s %p') as time

' 2017-05-12 15:05:19'