Skip to content

Instantly share code, notes, and snippets.

@ghitti
Last active January 31, 2020 14:16
Show Gist options
  • Save ghitti/c28d9ae71cacd551e8fe142ea4457e44 to your computer and use it in GitHub Desktop.
Save ghitti/c28d9ae71cacd551e8fe142ea4457e44 to your computer and use it in GitHub Desktop.
Read *csv File from IFS (IBM i)
-- Read *csv File from IFS
With x as (-- Split IFS File into Rows (at CRLF)
Select Ordinal_Position as RowKey, Element as RowInfo
from Table(SysTools.Split(Get_Clob_From_File('/home/myFile.csv'), x'0D25')) a
Where Trim(Element) > ''),
y as (-- Split IFS File Rows into Columns (and remove leading/trailing double quotes ")
Select x.*, Ordinal_Position ColKey,
Trim(B '"' from Element) as ColInfo
from x cross join Table(SysTools.Split(RowInfo, ',')) a)
-- Return the Result as Table
Select RowKey,
Min(Case When ColKey = 1 Then ColInfo End) EmployeeNo,
Min(Case When ColKey = 2 Then ColInfo End) Name,
Min(Case When ColKey = 3 Then ColInfo End) FirstName,
Min(Case When ColKey = 4 Then ColInfo End) Address,
Min(Case When ColKey = 5 Then ColInfo End) Country,
Min(Case When ColKey = 6 Then ColInfo End) ZipCode,
Min(Case When ColKey = 7 Then ColInfo End) City
From y
Where RowKey > 1 -- Remove header
Group By RowKey;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment