如何将字符串转换为日期时间格式 Mysql
How to convert string to date time format Mysql
我有这个文件 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 值。这是我的代码(时间是 table 列的名称)
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')
;
这是您应该使用的语法:
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
。
试试这个
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),'-'),''))
我有这个文件 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 值。这是我的代码(时间是 table 列的名称)
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')
;
这是您应该使用的语法:
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
。
试试这个
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),'-'),''))