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

Manutan combines digital services with the human touch to delight customers

Manutan combines digital services with the human touch to delight customers

At Manutan, we equip businesses and communities with the products and services they require to succeed. Headquartered in France, our company has three divisions, serving…

4 minute read

Reaching new markets in Europe and beyond

Reaching new markets in Europe and beyond

How information management specialists at One Fox slashed time to market for innovative products with OpenText Cloud Platform Services At One Fox, we’ve driven some…

4 minute read

SoluSoft helps government agencies tackle fraud faster

SoluSoft helps government agencies tackle fraud faster

Fraud, in all its forms, is a pervasive problem, spanning industries and preying on vulnerabilities in federal and state government systems. Each year in the…

3 minute read

Stay in the loop!

Get our most popular content delivered monthly to your inbox.