MySqlImport - 导入格式不正确的日期字段
我有一个 csv 文件,其日期字段的格式如下(以及其他字段):
17DEC2009
当我执行 mysqlimport 时,其他字段已正确导入,但此字段仍然是 0000-00-00 00:00:00
如何正确导入此日期?我是否必须先对文件运行 sed/awk 命令才能将其转换为正确的格式?如果是这样,那会是什么样子?月份是拼写出来的而不是数字这一事实重要吗?
I have a csv file that has a date field in a format like (among other fields):
17DEC2009
When I do a mysqlimport, the other fields are imported properly, but this field remains 0000-00-00 00:00:00
How can I import this date properly? Do I have to run a sed/awk command on the file first to put it into a proper format? If so, what would that be like? Does the fact that the month is spelled out instead of a number matter?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
STR_TO_DATE( ) 使您能够在查询中将字符串转换为正确的 DATE。它需要日期字符串和格式字符串。
检查手册条目中的示例以找出正确的格式。
我认为它应该沿着
%d%b%Y
的方向(但是 %b 应该产生像Dec
这样的字符串,而不是DEC
> 所以你必须尝试一下它是否有效)。STR_TO_DATE() enables you to convert a string to a proper DATE within the query. It expects the date string, and a format string.
Check the examples in the manual entry to figure out the correct format.
I think it should be along the lines of
%d%b%Y
(However the %b is supposed to produce Strings likeDec
instead ofDEC
so you will have to try out whether it works).我过去也遇到过这个问题。我要做的就是利用 LOAD DATA 并在此处设置适当的表达式 -
http://dev.mysql.com/doc/refman/5.1/en/load-data.html
I had this issue in the past. What I had to do was to utilize LOAD DATA and set the appropriate expression here -
http://dev.mysql.com/doc/refman/5.1/en/load-data.html
这是我解决类似问题所采取的方法。我的用例有点复杂,有这么多列,但在这里简单地展示解决方案。
我有包含 (Id int autogen, name varchar(100),DOB date) 的 Persons 表,并且需要从 CSV 文件填充几百万条数据(姓名、DOB)。
相同的逻辑甚至可以应用于其他类型的数据格式化,例如 double、time_stamp 等。
Here is the approach I took to solve similar problem. My use case was bit complex with so many columns, but making here simple to present the solution.
I have Persons table with (Id int autogen, name varchar(100),DOB date), and few million of data(name,DOB) needs to be populated from CSV file.
The same logic could be applied in doing even other kind of data formatting like double,time_stamp etc.