views:

186

answers:

3

Hi,

I am exporting a csv file in to mysql db using load data infile syntax. the date in csv is in 2009/10/31 7:8:57.0 format. Is there any way to convert this while loading to something like 2009-10-31 07:08:57 ?

A: 

(usual caveats apply here) A regular expression might be what you need. Substitute / with - and remove the trailing .0. I am surprised, though, that mysql can't handle dates like the one you provided. See for example the mySql manual. Have you tried feeding it to mysql and seeing what happens?

lorenzog
yes, it dislays as 0000:00:00 00:00:00
JPro
A: 

Execute TO_CHAR(TO_DATE(datefromcsv, 'YYYY/MM/DD HH:MI:SS.FF'), 'YYYY-MM-DD HH:MI:SS') when you are doing the INSERT into the db.

Lukman
thx for the info. I am using mysql and found that STR_TO_DATE will do thr trick.
JPro
owh I thought MySql use the same `TO_DATE` function like oracle. sorry :P
Lukman
A: 

2009/10/31 7:8:57.0 is an unusual date format. You can try to import data via script (a php script for example):

  • transform the date
  • generate an insert query for each row
  • execute the query

Alternatively, if its OK for you to edit the CSV data, I suggest that you use a formula or a date format to change the date format and export it into a new CSV file.

In excel, this formula should do the trick:

A1: 2009/10/31 7:8:57.0
B1: =SUBSTITUTE(A1,"/","-")
C1: =IF(ISERR(FIND(".",B1)),B1,LEFT(B1,FIND(".",B1)-1))
  • A1 contains the date as a string
  • B1 contains a partially cleaned-up date
  • C1 contains the cleaned-up date
Salman A
thx. i found the workaround. its STR_TO_DATE
JPro