<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	>
<channel>
	<title>Comments on: Exporting from MySQL to CSV</title>
	<atom:link href="http://www.simonwheatley.co.uk/2007/10/13/exporting-from-mysql-to-csv/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.simonwheatley.co.uk/feeder/?FeederAction=clicked&amp;feed=Articles+%28RSS2%29&amp;seed=http%3A%2F%2Fwww.simonwheatley.co.uk%2F2007%2F10%2F13%2Fexporting-from-mysql-to-csv%2F&amp;seed_title=Exporting+from+MySQL+to+CSV</link>
	<description></description>
	<pubDate>Sun, 20 Jul 2008 07:04:43 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.5</generator>
		<item>
		<title>By: Simon</title>
		<link>http://www.simonwheatley.co.uk/feeder/?FeederAction=clicked&amp;feed=Articles+%28RSS2%29&amp;seed=http%3A%2F%2Fwww.simonwheatley.co.uk%2F2007%2F10%2F13%2Fexporting-from-mysql-to-csv%2F&amp;seed_title=Exporting+from+MySQL+to+CSV#comment-35229</link>
		<dc:creator>Simon</dc:creator>
		<pubDate>Fri, 11 Apr 2008 06:52:03 +0000</pubDate>
		<guid isPermaLink="false">http://www.simonwheatley.co.uk/2007/10/13/exporting-from-mysql-to-csv/#comment-35229</guid>
		<description>Very handy Sam, thanks for sharing.</description>
		<content:encoded><![CDATA[<p>Very handy Sam, thanks for sharing.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Sam</title>
		<link>http://www.simonwheatley.co.uk/feeder/?FeederAction=clicked&amp;feed=Articles+%28RSS2%29&amp;seed=http%3A%2F%2Fwww.simonwheatley.co.uk%2F2007%2F10%2F13%2Fexporting-from-mysql-to-csv%2F&amp;seed_title=Exporting+from+MySQL+to+CSV#comment-35187</link>
		<dc:creator>Sam</dc:creator>
		<pubDate>Wed, 09 Apr 2008 11:38:48 +0000</pubDate>
		<guid isPermaLink="false">http://www.simonwheatley.co.uk/2007/10/13/exporting-from-mysql-to-csv/#comment-35187</guid>
		<description>I just spent a few hours figuring this one out and there doesn't seem to be much info online about it so I thought I'd share.

You should use the following syntax to create a CSV file in the format expected by Microsoft Excel:

... INTO OUTFILE '/temp.csv' FIELDS ESCAPED BY '""' TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\r\n';

However fields with carriage returns may break the CSV as MySQL will automatically close a field when the \r\n line break is found. To work around this, replace all \r\n breaks with \n. The field does not close on \n breaks and it will be read into a single cell in Excel. You can do this in the same SQL statement, for example:

SELECT REPLACE(field_with_line_breaks, '\r\n', '\n') FROM table INTO OUTFILE '/temp.csv' FIELDS ESCAPED BY '""' TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\r\n';

I also found that null values could break the CSV. These can be handled in a similar way:

SELECT IFNULL(possible_null_field, "") FROM table INTO OUTFILE '/temp.csv' FIELDS ESCAPED BY '""' TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\r\n';

Note: this replaces NULL values with an empty string which is technically not the same thing but it will give you an empty cell in Excel instead of breaking the CSV structure and shifting the following cells to the left.</description>
		<content:encoded><![CDATA[<p>I just spent a few hours figuring this one out and there doesn&#8217;t seem to be much info online about it so I thought I&#8217;d share.</p>
<p>You should use the following syntax to create a CSV file in the format expected by Microsoft Excel:</p>
<p>&#8230; INTO OUTFILE &#8216;/temp.csv&#8217; FIELDS ESCAPED BY &#8216;&#8221;"&#8216; TERMINATED BY &#8216;,&#8217; ENCLOSED BY &#8216;&#8221;&#8216; LINES TERMINATED BY &#8216;\r\n&#8217;;</p>
<p>However fields with carriage returns may break the CSV as MySQL will automatically close a field when the \r\n line break is found. To work around this, replace all \r\n breaks with \n. The field does not close on \n breaks and it will be read into a single cell in Excel. You can do this in the same SQL statement, for example:</p>
<p>SELECT REPLACE(field_with_line_breaks, &#8216;\r\n&#8217;, &#8216;\n&#8217;) FROM table INTO OUTFILE &#8216;/temp.csv&#8217; FIELDS ESCAPED BY &#8216;&#8221;"&#8216; TERMINATED BY &#8216;,&#8217; ENCLOSED BY &#8216;&#8221;&#8216; LINES TERMINATED BY &#8216;\r\n&#8217;;</p>
<p>I also found that null values could break the CSV. These can be handled in a similar way:</p>
<p>SELECT IFNULL(possible_null_field, &#8220;&#8221;) FROM table INTO OUTFILE &#8216;/temp.csv&#8217; FIELDS ESCAPED BY &#8216;&#8221;"&#8216; TERMINATED BY &#8216;,&#8217; ENCLOSED BY &#8216;&#8221;&#8216; LINES TERMINATED BY &#8216;\r\n&#8217;;</p>
<p>Note: this replaces NULL values with an empty string which is technically not the same thing but it will give you an empty cell in Excel instead of breaking the CSV structure and shifting the following cells to the left.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
