Export to CSV File With Fields Enclosed by Quotes

It’s easy to export table data to a CSV file using vsql. Although there isn’t a VSQL option to enclose each output field in quotes,…

OpenText  profile picture
OpenText

March 29, 20201 min read

It’s easy to export table data to a CSV file using vsql. Although there isn’t a VSQL option to enclose each output field in quotes, you can accomplish that task with the Linux sed command.

Example:

[dbadmin@SE-Sandbox-26-node1 ~]$ vsql -c "SELECT * FROM csv_example;"
 c1 |  c2   |   c3
----+-------+--------
  1 | DATA1 | 1.2734
  2 | DATA2 | 23.232
(2 rows)

[dbadmin@SE-Sandbox-26-node1 ~]$ vsql -F ',' -P footer=off -Aqc "SELECT * FROM csv_example;" | sed -e 's/[\"]/\\&/g' -e 's/,/"&"/g' -e 's/^\|$/"/g'
"c1","c2","c3"
"1","DATA1","1.2734"
"2","DATA2","23.232"

Once the formatting looks good, you can redirect the output to your CSV file:

[dbadmin@SE-Sandbox-26-node1 ~]$ vsql -F ',' -P footer=off -Aqc "SELECT * FROM csv_example;" | sed -e 's/[\"]/\\&/g' -e 's/,/"&"/g' -e 's/^\|$/"/g' > csv_example.csv

[dbadmin@SE-Sandbox-26-node1 ~]$ cat csv_example.csv
"c1","c2","c3"
"1","DATA1","1.2734"
"2","DATA2","23.232"

Helpful Links:
Exporting Data Using VSQL
VSQL Field Separator

Have fun!

Share this post

Share this post to x. Share to linkedin. Mail to
OpenText avatar image

OpenText

OpenText, The Information Company, enables organizations to gain insight through market-leading information management solutions, powered by OpenText Cloud Editions.

See all posts

More from the author

The Agentic AI Genome: Turning AI investment into enterprise advantage

The Agentic AI Genome: Turning AI investment into enterprise advantage

Read about how to unleash the power of AI

April 10, 2026

4 min read

Elevate together at OpenText Summit London 2026

Elevate together at OpenText Summit London 2026

Take control of your AI journey

April 09, 2026

3 min read

ServiceNow Edge Encryption replacement

ServiceNow Edge Encryption replacement

What organizations need to know before 2028

April 08, 2026

6 min read

Stay in the loop!

Get our most popular content delivered monthly to your inbox.