Midrange News for the IBM i Community


Posted by: Bob Cozzi
Rogue Programmer
Cozzi Productions, Inc.
Chicagoland
CSV_VAL() Extract CSV Column Value
© Bob Cozzi, Jr. has no ratings.
Published: 10 Jun 2019
Revised: 20 Jun 2019 - 7 days ago
Last viewed on: 25 Jun 2019 (15 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_VAL() - UDF Published by: Bob Cozzi on 10 Jun 2019 view comments
© Bob Cozzi, Jr.

Extract CSV Value

The iQuery.CSV_VAL() UDF reads the CSV result set buffer and extracts and then returns the value for the column identified on the 2nd parameter.

The returned value is always a varchar(2048) value and my be re-cast using CAST or by wrapping the result in a 2nd VARCHAR with a different result column width.

Parameters

iQuery.csv_val( data, 'column name' )
iQuery.csv_val( data, relative-column-number )

The data parameter identifies the name of the result set buffer contains the raw CSV data. This must be the column named DATA that is returend by the iQuery.CSV() UDTF. Any other value will cause unpredictable results to occur.

A column name or relative column number may be used to identify the CSV column you wish to extract. 

When a relative column number is specified, the value must be between 1 and the maximum number of CSV columns in the CSV file.

When a column name is specified, the name may be specified with or without embedded blanks. This provides more flexibility is the column naming when producing the CSV file itself. For example if the column name in the CSV file is "Customer Number" any of the following values may be speciied on the CSV_VAL UDF to identify the column by name:

  • csv_val(data, 'Customer Name')
  • csv_val(data, 'customername')
  • csv_val(data, 'customer      NAME')

Blanks are ignored in both the CS_VAL UDF and the CSV file column headings.

Column
Name
System
Column
Name
Data Type Description
CSV_VAL CSV_VAL VARCHAR(2048) The column value is returned as a varchar value to the result set. The length will be 2k unless the user overrides that length using the cast or similar operation. For example: (cast csv_val(data,'custname') as varchar(30))
       

Example

select csv_val(data,'custno'), varchar(csv_val(data,'custName'),32) 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

COMMENTS