Midrange News for the IBM i Community

Posted by: Bob Cozzi
Rogue Programmer
Cozzi Productions, Inc.
CSV() - Read and Parse Comma Separated Values File
© Bob Cozzi has no ratings.
Published: 10 Jun 2019
Revised: 20 Jun 2019 - 90 days ago
Last viewed on: 16 Sep 2019 (181 views) 

Using IBM i? Need to create Excel, CSV, HTML, JSON, PDF, SPOOL reports? Learn more about the fastest and least expensive tool for the job: SQL iQuery.

iQuery.CSV() UDTF Published by: Bob Cozzi on 10 Jun 2019 view comments
© Bob Cozzi

Read CSV File

The iQuery.CSV() UDTF reads an IFS text file that contains Comma Separated Values and returns that contant to the caller. The data can be extracted using the provided iQuery.csv_val() function or one of the variations of it to return decimal, text, date/time or integer content.

The maximum width of a CSV result cannot exceed about 8k (8192 bytes).


iQuery.csv( 'ifs-file-and-path', [number of header rows: dft 1], [ dump column names] )

The IFS file and path is a classic IFS file name with the fully qualified path name. If no path is specified and only a file name is used, it uses the current working directory to locate the file. It is normally recommended to fully qualify the file name to the path in which it is located.

The optional number of header rows indicates to the UDTF how many row were used to generate the CSV column headings (if any). By default 1 row is assumed.  The column headings may be used to reference the columns using the CSV_VAL() and related functions. If no column headings are included in the CSV file, then specify 0 for this parameter, and refer to the columns on the CSV_VAL() function by the relative column number only.

The optional dump column name parameter may be used to generate a list of CSV column names to the joblog. This can help with debugging certain files when the column name is not working as expected. Typically this is rarely used an is only inteded for debug purposes.

Data Type Description
DATA DATA VARCHAR(8192) The CSV data is parsed and returned as a fixed width record. Use this columin the CSV_VAL() UDF to extract the data from the result set using either column names or relative column number. 


select csv_val(data,'custno'), csv_val(data,'custName') from iQuery.csv('/home/pickles/customers.csv');

In this example, the CUSTNO and CUSTNAME columns from the CSV file named CUSTOMERS.CSV are returned. The file is located in folder /home/pickles on the IFS. The data is returned as text. To return the CUSTNO colum as numeric, use CSV_DEC or CSV_INT functions.


Return to midrangenews.com home page.
Sort Ascend | Descend