Question

So I'm using to run the following query to dump my table to a CSV file:

SELECT * INTO OUTFILE 'testout.csv'
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '\"'
LINES TERMINATED BY '\n'
FROM dbname.tblname;

This works fine, but all of the fields are being surrounded by double quotes. I'd like to get it so that only the varchar (i.e. string) fields are surrounded by quotes in the output. Is there any easy way to do this? It would probably involve type-checking fields in the query somehow, but I don't have the slightest clue for how to approach this. Any help would be greatly appreciated.

Was it helpful?

Solution

When you have OPTIONALLY ENCLOSED BY it should already do as you want:

If you specify OPTIONALLY, the ENCLOSED BY character is used only to enclose values from columns that have a string data type (such as CHAR, BINARY, TEXT, or ENUM):

1,"a string",100.20
2,"a string containing a , comma",102.20
3,"a string containing a \" quote",102.20
4,"a string containing a \", quote and comma",102.20

Are you sure that you don't have varchar columns for every column?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top