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 minute 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

Fax and figures – automate your fax processes for maximum productivity

Fax and figures – automate your fax processes for maximum productivity

Manual fax processing isn’t scalable

4 minute read

Wheels and Wings: OpenText and Jaguar TCS Racing Accelerate into the Future

Wheels and Wings: OpenText and Jaguar TCS Racing Accelerate into the Future

Dive into the future with OpenText & Jaguar TCS Racing, where data & AI drive innovation on and off the track!

3 minute read

Driving success on the historic Monaco Circuit

Driving success on the historic Monaco Circuit

OpenText real-time data analytics powers Jaguar TCS Racing

4 minute read

Stay in the loop!

Get our most popular content delivered monthly to your inbox.

Sign up