简体   繁体   English

如何在批量插入之前将.csv文件中的日期转换为SQL格式

[英]How to convert date in .csv file into SQL format before mass insertion

I have a csv file with a couple thousand game dates in it, but they are all in the MM/DD/YYYY format 我有一个csv文件,里面有几千个游戏日期,但它们都是MM / DD / YYYY格式

2/27/2011,3:05 PM,26,14

(26 and 14 are team id #s), and trying to put them into SQL like that just results in 0000-00-00 being put into the date field of my table. (26和14是团队ID #s),并尝试将它们放入SQL中,这只会导致将0000-00-00放入我的表的日期字段中。 This is the command I tried using: 这是我尝试使用的命令:

LOAD DATA LOCAL INFILE 'c:/scheduletest.csv' INTO TABLE game
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
(`date`, `time`, `awayteam_id`, `hometeam_id`);

but again, it wouldn't do the dates right. 但同样,它不会做正确的日期。 Is there a way I can have it convert the date as it tries to insert it? 有没有办法让它在尝试插入日期时转换日期? I found another SO question similar to this, but I couldn't get it to work. 我发现了另一个与此类似的问题 ,但我无法让它发挥作用。

Have you tried the following: 您是否尝试过以下方法:

LOAD DATA LOCAL INFILE 'c:/scheduletest.csv' INTO TABLE game
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
(@DATE_STR, `time`, `awayteam_id`, `hometeam_id`)
SET `date` = STR_TO_DATE(@DATE_STR, '%c/%e/%Y');

For more information, the documentation has details about the use of user variables with LOAD DATA (about half-way down - search for "User variables in the SET clause" in the page) 有关更多信息,该文档包含有关使用LOAD DATA用户变量的详细信息(大约一半 - 在页面中搜索“SET子句中的用户变量”)

You can use variables to load the data from the csv into and run functions on them before inserting, like: 在插入之前,您可以使用变量将csv中的数据加载到它们并在其上运行函数,如:

LOAD DATA INFILE 'file.txt'
INTO TABLE t1
(@datevar, @timevar, awayteam_id, hometeam_id)
SET date = STR_TO_DATE(@datevar, '%m/%d/%Y'),
SET time = etc etc etc;

If you file is not too big, you can use the Excel function TEXT. 如果文件不是太大,可以使用Excel函数TEXT。 If, for example, your date is in cell A2, then the formula in a temporary column next to it would be =TEXT(A2,"yyyy-mm-dd hh:mm:ss"). 例如,如果您的日期在单元格A2中,则其旁边的临时列中的公式将为= TEXT(A2,“yyyy-mm-dd hh:mm:ss”)。 This will do it and then you can paste the values of the formula's result back into the column and then delete the temporary column. 这样做,然后您可以将公式结果的值粘贴回列中,然后删除临时列。

My suggestion would be to insert the file into a temporary holding table where the date column is a character datatype. 我的建议是将文件插入临时保存表,其中date列是字符数据类型。 Then write a query with the STR_TO_DATE conversion to move the data from the holding table to your final destination. 然后使用STR_TO_DATE转换编写查询,以将数据从保留表移动到最终目标。

  1. Convert field that you are using for the date to varchar type so it will play friendly with any format 将您用于日期的字段转换为varchar类型,以便它可以使用任何格式播放

  2. Import CSV 导入CSV

  3. Convert the dates to a valid mysql date format using something like: 使用以下内容将日期转换为有效的mysql日期格式:

UPDATE table SET field = STR_TO_DATE(field, '%c/%e/%Y %H:%i');
  1. Then revert field type to date 然后将字段类型还原为日期

Use a function to convert the format as needed. 使用函数根据需要转换格式。

I'm not an expert on MySQL, but http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_str-to-date looks promising. 我不是MySQL的专家,但http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_str-to-date看起来很有前途。

If you can't do that in the load command directly, you may try creating a table that allows you to load all the values as VARCHAR and then to do an insert into your game table with a select statement with the appropriate conversion instead. 如果您不能直接在load命令中执行此操作,则可以尝试创建一个表,该表允许您将所有值加载为VARCHAR ,然后使用带有相应转换的select语句插入到game表中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM