Export data to CSV using nzsql

Nikhil Titus picture Nikhil Titus · Apr 17, 2015 · Viewed 11.3k times · Source

I would like to export data as CSV from netezza. The data consists of millions of lines of records. The data should be within inverted commas and should have ^ as delimiter. Eg: "a"^"b"^"c"

Answer

ScottMcG picture ScottMcG · Apr 18, 2015

Whenever you want to export data from Netezza, you will want to use its external tables functionality.

If you are exporting to a filesystem mounted locally to the Netezza host, you could use this:

CREATE external TABLE '/tmp/test_export.txt' USING (delimiter '^') AS
SELECT *
FROM test_export;

If you are connecting to Netezza via JDBC, ODBC, or OLE-DB via tool like Aginity Workbench, and want to export the data locally to your workstation, you could use this:

CREATE external TABLE 'c:\test_export.txt' USING (delimiter '^' remotesource odbc) AS
SELECT *
FROM test_export;

Unfortunately, there is is no external table option that will allow you to also wrap every column in quotation marks. You will have to do that explicitly with a concatenation in the SQL like this:

CREATE external TABLE 'c:\test_export.txt' USING (delimiter '^' remotesource odbc) AS
SELECT  '"' || col1 || '"',
        '"' || col2 || '"'
FROM test_export;

You can also use the nzsql CLI interface with the following options to achieve something similar, but it is quite a bit slower. For example, on my system, using the external table method to export about 2 million rows, which creates an export file about 3.5 GB in size, takes 20 seconds. With the CLI method it took 3about 180 seconds.

nzsql -d DB_NAME -F "^" -t -A -o export.txt  -c "select * from test_export"