在使用MySQL的LOAD DATA LOCAL INFILE导入CSV时,如何将字符串日期更改为MySQL日期格式 [英] How to change string date to MySQL date format at time of import of CSV using MySQL's LOAD DATA LOCAL INFILE

查看:204
本文介绍了在使用MySQL的LOAD DATA LOCAL INFILE导入CSV时,如何将字符串日期更改为MySQL日期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用MySQL的LOAD DATA LOCAL INFILE SQL语句将数据从CSV文件加载到现有的数据库表中。

I'm using MySQL's LOAD DATA LOCAL INFILE SQL statement to load data from a CSV file into an existing database table.

下面是一个示例SQL语句: / p>

Here is an example SQL statement:

LOAD DATA LOCAL INFILE 'file.csv' INTO TABLE my_table
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
(name, address, dateOfBirth)

映射到dateOfBirth字段的CSV中的第三列当前具有以下格式的日期:

The third column in the CSV that maps to the dateOfBirth field currently has the date in the following format:

14-Feb-10

如何修改上述SQL语句将日期格式化为MySQL的日期格式即 2010-02-14

How can I modify the above SQL statement to format the date into MySQL's date format i.e. 2010-02-14?

我知道如何使用正常的INSERT语法转换字符串日期:

I know how to convert a string date when using normal INSERT syntax using:

STR_TO_DATE('14 -Feb-10','%d-%b-%y')

推荐答案

您需要使用 SET 一个变量来引用该列的行内容。在您的列列表中,您将日期列分配给变量名称。然后,您可以在 SET 语句中使用它。 (注意,我没有MySQL在我面前测试这个。)

You need to use the SET clause, along with a variable to reference the contents of the row at that column. In your column list, you assign your date column to a variable name. You can then use it in your SET statement. (Note, I haven't got MySQL in front of me to test this on.)

LOAD DATA LOCAL INFILE 'file.csv' INTO TABLE my_table
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
(name, address, @var1)
set dateOfBirth = STR_TO_DATE(@var1, '%d-%b-%y')

请参阅以下示例: http: //mysql2.mirrors-r-us.net/doc/refman/5.1/en/load-data.html (不知道为什么这个页面似乎与主要文档有所不同,因为它实际上包含一个 SET usage。)

See examples a way down the page at: http://mysql2.mirrors-r-us.net/doc/refman/5.1/en/load-data.html (Not sure why this page seems to differ from the main documentation in that it actually contains an example of SET usage.)

这篇关于在使用MySQL的LOAD DATA LOCAL INFILE导入CSV时,如何将字符串日期更改为MySQL日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆