简体   繁体   中英

Openoffice-calc can't change the date format of a column

In openoffice calc I open a csv file downloaded from the internet.

One of the columns is for date. The date is in format dd/mm/yy. It is also aligned left (which I think it is saved as text).

I want to modify the date format to yyyy-mm-dd so i can fit it in a mysql table that i have created and in which i hava a column with data type date (so it requires the format i mentioned before).

So I select the column 'Date' in openoffice calc and try to change the format from what it is to the one i want. Nothing is happening. No changes at all.

Any suggestion? Has anything to do with character set or anything like that?

If you select one cell and look at the cell contents in the top 'formula' box - does it start with a single quote ? That means, it is imported as text. Try remove the quote, and the date suddenly behaves as a date (and uses your display preferences for that cell).

There is a funny hack to remove these leading quotes using search and replace. Select your column, do a search and replace, click more options, current selection only, use regular expressions :

search ^(.*)$
replace &

in regexp speak, this means search anything and replace it by itself . in OO, this removes the leading quote.

You could just use MySQL's STR_TO_DATE() function:

LOAD DATA INFILE '/path/to/file.csv'
  INTO TABLE my_table
  CHARACTER SET utf8
  FIELDS
    TERMINATED BY ','
    OPTIONALLY ENCLOSED BY '"'
  LINES
    TERMINATED BY '\r\n'
  IGNORE 1 LINES
  (@date, col_a, col_b, etc)
SET
  date_col = STR_TO_DATE(@date, '%d/%m/%Y')
;

它在导入/打开对话框中,您必须告诉它这些列是一个日期,然后一切正常。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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