Skip to content

Instantly share code, notes, and snippets.

@dancarlosgabriel
Forked from BirgittaHauser/ReadCsv.SQL
Created January 31, 2020 23:03
Show Gist options
  • Save dancarlosgabriel/3bb918419a1fe4830a998fc0b2f7ebfc to your computer and use it in GitHub Desktop.
Save dancarlosgabriel/3bb918419a1fe4830a998fc0b2f7ebfc to your computer and use it in GitHub Desktop.
Read *.csv File directly with SQL
-- 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/Hauser/Employee.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