2

Usually, to export CSV reports from a mysql server, I connect & run a query on the server, save the results into a text file, and import the file to excel by specifying to separate the columns using the pipe delimited character.

I need to run a report on a table with a MEDIUMTEEXT column type, which contains comma characters, and line breaks (\n).

The line breaks and commas symbols are breaking the table layout.

SELECT `number`,REPLACE(`description`, ',', ''),  mr.`dateInserted` FROM 
`mr`  WHERE mr.dateInserted >= '2012-01-01' AND mr.dateInserted <= '2012-01-31'

I solved the comma issues by using the MySQL REPLACE function, however, how can I remove the line breaks from the results?

3 Answers 3

2

You can do this with an query, here's an example.

SELECT
  id,
  name,
  email
INTO
  OUTFILE '/tmp/result.csv'
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
ESCAPED BY ‘\\’
LINES TERMINATED BY '\n'
FROM users WHERE 1
Sign up to request clarification or add additional context in comments.

1 Comment

I think i tried that, the problem with that approach is that it saves the file locally, and it's an amazon RDS instance
1

Char(13) is a carriage return

Char(10) is a line feed

Try nested replace functions for path of least resistance :)

SELECT 
    `number`, 
    REPLACE(REPLACE(REPLACE(`description`, ',', ''), CHAR(13),''), CHAR(10),''),
    mr.`dateInserted`
FROM
    `mr`
WHERE
    mr.dateInserted >= '2012-01-01' 
AND 
    mr.dateInserted <= '2012-01-31'

Comments

1

You can also use a combination of a "view" and "mysqldump".

http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html

1 Comment

VERY NICE! I didn't know that!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.