csv mysql 日期格式,CSV日期格式到MySQL日期格式

When I try to import my CSV file to MySQL, all the date become 0000-00-00.

CSV date format is DD/MM/YYYY like this 16/11/2016.

MySQL format is: YYYY/MM/DD. I am using phpmyadmin to import this. How to solve this date transfer?

解决方案

The format DD/MM/YYYY is not a valid date in MySQL. You could use LOAD DATA along with STR_TO_DATE to parse the date strings into actual dates which MySQL can recognize:

LOAD DATA INFILE 'path/to/file.csv'

INTO TABLE yourTable FIELDS TERMINATED BY ',' LINES TERMINATED BY '\r\n'

(

col1, col2, @var1, col4

)

SET date_col = STR_TO_DATE(@var1, '%d/%m/%Y')

The above assumes that your table has 4 columns, and that the third column you are reading in from your CSV file is the troublesome date. The trick here is that each date string will get mapped on the fly into a valid date type, leaving you with the result you want.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值