CSVWrite with Quotes on certain data columns

122 views
Skip to first unread message

Jason

unread,
May 25, 2016, 10:27:45 AM5/25/16
to H2 Database
I'm attempting to extract data into a CSV where one or more columns may contain a description that I would like surrounded by quotation marks.  Without any CSVOptions each column of data is enclosed in quotation marks so I set the field delimiter to blank which effectively eliminates part of the problem.  The query I'm executing is below

call csvwrite('[some_file_path]','
select A, B, description
from SOME_TABLE
where A like ''x%''
order by A, B
', 'fieldDelimiter= ');

Because the descriptions column may have commas in it, i'd like to place quotes around it and have updated the query as follows

call csvwrite('[some_file_path]','
select A, B, ''"'' || description ''"'' as description
from SOME_TABLE
where A like ''x%''
order by A, B
', 'fieldDelimiter= ');

but the resulting output is

A,B,Description
val1,val2,""some description""

containing two sets of quotes on each side of the string.  Not what I was expecting, so I tried

select A, B, chr(34) || description || chr(34) as description

and the result is the same.

How do I go about producing

A,B,Description
val1,val2,"some description"

Noel Grandin

unread,
May 26, 2016, 3:35:13 AM5/26/16
to h2-da...@googlegroups.com
Unfortunately CSVWRITE is not very flexible. If it doesn't meet your needs as is, you will need to hand-roll your own
export mechanism.

I am tempted to write some kind of RESULTWRITE() function, which will let people simply pipe their resultset to a file,
then they can do their own escaping and fiddling with formats.

Jason

unread,
May 26, 2016, 10:22:46 AM5/26/16
to H2 Database
ok, thanks for the follow-up.  RESULTWRITE sounds like a good idea.  I'll keep my eye out for future enhancements.
Reply all
Reply to author
Forward
0 new messages