MySQL export into outfile : CSV escaping chars MySQL export into outfile : CSV escaping chars sql sql

MySQL export into outfile : CSV escaping chars


I think your statement should look like:

SELECT id,    client,   project,   task,   description,    time,   date    INTO OUTFILE '/path/to/file.csv'  FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'  LINES TERMINATED BY '\n'  FROM ts

Mainly without the FIELDS ESCAPED BY '""' option, OPTIONALLY ENCLOSED BY '"' will do the trick for description fields etc and your numbers will be treated as numbers in Excel (not strings comprising of numerics)

Also try calling:

SET NAMES utf8;

before your outfile select, that might help getting the character encodings inline (all UTF8)

Let us know how you get on.


Here is what worked here:Simulates Excel 2003 (Save as CSV format)

SELECT REPLACE( IFNULL(notes, ''), '\r\n' , '\n' )   AS notesFROM sometablesINTO OUTFILE '/tmp/test.csv' FIELDS TERMINATED BY ',' ENCLOSED BY '"' ESCAPED BY '"'LINES TERMINATED BY '\r\n';
  1. Excel saves \r\n for line separators.
  2. Excel saves \n for newline characters within column data
  3. Have to replace \r\n inside your data first otherwise Excel will think its a start of the next line.


What happens if you try the following?

Instead of your double REPLACE statement, try:

REPLACE(IFNULL(ts.description, ''),'\r\n', '\n')

Also, I think it should be LINES TERMINATED BY '\r\n' instead of just '\n'