如何将字符串转换为日期时间格式Mysql

时间:2017-02-28 10:48:33

标签: mysql str-to-date

我有这个文件csv:

20170117-00:00:6    3087A6B282A46C  124.191.134.139 2746    256 63628   
20170117-00:00:53   330391C9F58B5A  120.18.97.172   2746    257 43077
20170117-00:02:27   3303CD14BD40C8  110.147.131.78  2746    256 63628
20170117-00:02:00   3304B3CD448E4F  189.35.218.62   2746    255 43074

我正在尝试使用本地infile的加载数据上传它,但我在时间列中只获得Null值。这里有我的代码(时间是表格列的名称)

LOAD DATA LOCAL INFILE 'path.log'
                INTO TABLE Clicks
                CHARACTER SET latin1
                FIELDS TERMINATED BY '\t'

                ENCLOSED BY '"'
                LINES TERMINATED BY '\n'
                IGNORE 1 ROWS

                SET Time= STR_TO_DATE(@Time,'%Y%m%d-%H:%i:%s')
                ;

2 个答案:

答案 0 :(得分:0)

这是您应该使用的语法:

LOAD DATA LOCAL INFILE 'path.log'
INTO TABLE Clicks
CHARACTER SET latin1
FIELDS TERMINATED BY '\t'
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 ROWS
(@Time, column2, column3, column4, column5)         -- read time data into @Time
SET column1 = STR_TO_DATE(@Time, '%Y%m%d-%H:%i:%s') -- then set column1

我认为@Time变量从未被设置为每个正在读取的记录的任何内容,因此您在第一列中获得了NULL

答案 1 :(得分:0)

试试这个

LOAD DATA LOCAL INFILE 'path.log'
                INTO TABLE Clicks
                CHARACTER SET latin1
                FIELDS TERMINATED BY '\t'

                ENCLOSED BY '"'
                LINES TERMINATED BY '\n'
                IGNORE 1 ROWS

                SET TIME= CONCAT(SUBSTRING(@Time,1,4),'-',SUBSTRING(@Time,5,2),'-',SUBSTRING(@Time,7,2),' ',REPLACE(@Time,CONCAT(SUBSTRING_INDEX(@Time, '-',1),'-'),''))