Skip to content

Instantly share code, notes, and snippets.

View NielsLiisberg's full-sized avatar

Niels Liisberg NielsLiisberg

View GitHub Profile
@NielsLiisberg
NielsLiisberg / setenvvar.sql
Last active January 24, 2024 12:55
SQL: set environment variable
-- Set environment variable from a string value
-- you need library QSYSINC installed:
-- https://www.ibm.com/support/knowledgecenter/ssw_ibm_i_74/apiref/conQSYSINC.htm
--
-- Simply paste this gist into ACS SQL and step through the example.
--
-- It is a cool example how far you can go with SQL: Have fun -
-- (C) Niels Liisberg 2023
--
-- This gist is distributed on an "as is" basis, without warranties
@NielsLiisberg
NielsLiisberg / to_num.sql
Last active January 1, 2024 10:07
TO_NUM is the TO_NUMBER in a lax and european version
-- Convert text to decimal number for europeans where decimal point is a comma
--
-- This also showcase how to integrate the C code directly into your UDTF
--
-- Simply paste this gist into ACS SQL and step through the example.
--
-- Note: I am using the library QUSRSYS. I suggest you put it into your own tool library
--
-- It is a cool example how far you can go with SQL: Have fun 😀
-- (C) Niels Liisberg 2022
@NielsLiisberg
NielsLiisberg / copy_source_to_ifs.sql
Last active December 11, 2023 11:03
SQL - Export source files members to IFS and preserving member text
-- Copy members from a source physical file to IFS directory and preserving the member text as part of the filename.
--
-- Notes:
-- 1) I am using library QUSRSYS. I suggest you put it into your own tool library
-- 2) The output direcory has to exists.
-- 3) Files contents are being replaces if they already exists.
-- 4) IFS files CCSID is set to 1208 that is UTF-8
--
-- Simply paste this gist into ACS SQL and step through the example.
--
@NielsLiisberg
NielsLiisberg / uuid.sql
Created November 20, 2023 11:13
SQL generate GUID / UUID
-- Use MI to generarte a RFC 4122 compiant UUID / GUID
--
-- This also showcase how to integrate the MI api using
-- C code directly into your UDF
--
-- Note: I am using library QUSRSYS. I suggest you put it into your own tool library
--
-- Simply paste this gist into ACS SQL and step through the example.
--
-- It is a cool example how far you can go with SQL: Have fun -
@NielsLiisberg
NielsLiisberg / ifs_access.sql
Last active October 24, 2023 10:58
SQL - Check access to an IFS file or folder
-- SQL Scalar function to check access to an IFS file or folder
-- It returns:
-- 0=If the file or folder Exists and you have access
-- -1=No access or file does not exists
--
-- This is a wrapper for the access() unix API:
-- https://pubs.opengroup.org/onlinepubs/009695299/functions/access.html
-- Note second parameter is the acces mode, that defaults to F_OK
@NielsLiisberg
NielsLiisberg / ifs_delete_file.sql
Created October 11, 2023 12:05
SQL - Delete (unlink) a IFS file
-- SQL Scalar function to delete ( unlink) a IFS files
-- this is a wrapper for unlink (delete IFS files) system / unix API
-- Simply paste this gist into ACS SQL and run it to create the UDTF.
-- Note: I am using library QUSRSYS. I suggest you put it into your own tool library
-- It is a cool example how far you can go with SQL: Have fun -
-- (C) Niels Liisberg 2023
@NielsLiisberg
NielsLiisberg / capitalize.sql
Last active September 1, 2023 09:18
UDTF to return a capitalize first letter in each word of a string
-- UDTF to return a capitalize first letter in each word of a string
-- Simply paste this gist into ACS SQL and run it to create the UDTF.
-- Note: I am using library QUSRSYS. I suggest you put it into your own tool library
-- It is a cool example how far you can go with SQL: Have fun -
-- (C) Niels Liisberg 2023
@NielsLiisberg
NielsLiisberg / dspdbr
Last active August 16, 2023 14:53
List database relations with keys for files/tables
-- Returns database relations with keys
-- for SQL tables, the name is convert to the physical filename first.
-- Simply paste this gist into ACS SQL and run it to create the UDTF.
-- Note: I am using library QUSRSYS. I suggest you put it into your own tool library
-- It is a cool example how far you can go with SQL: Have fun -
-- (C) Niels Liisberg 2023
@NielsLiisberg
NielsLiisberg / udtf-intro.sql
Last active June 13, 2023 12:22
SQL Db2 for IBM i - introduction to UDTF
-- SQL Db2 for IBM i - introduction to UDTF
--
-- This is a tutorial starting with SQL functions, over User defined Table Function (UDTF)
-- and procedures. It covert the basic features and some common pitfalls.
-- By using IBM i SQL service as the vehicle for making service calls over http and integrate data
-- from Db2 on the IBM i makes this tutorial super relevant.
--
-- It is a cool example how far you can go with SQL: Have fun -
-- (C) Niels Liisberg 2022
@NielsLiisberg
NielsLiisberg / bash_table_bin.sql
Last active March 31, 2023 11:50
SQL Run bash scripts and return the data to SQL in ASCII
-- Run a bash command or script and returns the stdout as a table.
-- Each text line will be a row in the result set - the magic is done by the cool "pipe" statement.
-- Data is returned in binay (ASCII) so you need the ASC_TOO_EBCDIC to look at at the rows from with in ACS
-- So if you just heed to run bash text then use my gist bash_table
--
-- It assumes bash is installed by YUM so
-- it will be in the default location /QOpenSys/pkgs/bin/bash
-- You can use this aproach to other shells like sh, qsh setc.
-- You need to have the "ifs_write" procedure found on my gist
-- You also need library QSYSINC installed: