Skip to content

Instantly share code, notes, and snippets.

View forstie's full-sized avatar

Scott Forstie forstie

  • IBM
View GitHub Profile
@forstie
forstie / Query Spooled File contents for a specific user.sql
Last active May 2, 2024 07:55
Query Spooled File contents for a specific user
--
-- description: What spooled files does the current user own?
--
select job_name, spooled_file_name, file_number, user_data,
create_timestamp
from qsys2.output_queue_entries_basic
where user_name = user;
stop;
--
-- description: Query the contents of RUNSQLSTM spooled files for the current user
@forstie
forstie / Dates and TIMESTAMP_FORMAT
Last active May 2, 2024 07:55
Formatting date data into true date and time date types
-- Author: Scott Forstie
-- Email: forstie@us.ibm.com
create or replace variable coolstuff.decdate dec(6,0);
set coolstuff.decdate = '190718';
-- July 18, 2019 (yes, really!)
values timestamp_format(varchar(coolstuff.decdate), 'YYMMDD');
-- Wow
-- Yowza
@forstie
forstie / JSON_TABLE and survival tips for shredding JSON with SQL
Last active May 2, 2024 07:55
This example shows how to overcome what seems to be commonplace: JSON Web Services that return an invalid JSON document.
-- This fails to return data....why?
SELECT cusip, issueDate, bidToCoverRatio
FROM JSON_TABLE(
SYSTOOLS.HTTPGETCLOB('https://www.treasurydirect.gov/TA_WS/securities/announced?format=json&type=FRN&pagesize=5', null),
'$.root[*]'
COLUMNS(cusip VARCHAR(10) PATH '$.cusip',
issueDate Timestamp PATH '$.issueDate',
bidToCoverRatio double PATH '$.bidToCoverRatio')
) AS X;
stop;
@forstie
forstie / Generating spreadsheets with SQL.sql
Last active May 2, 2024 07:54
In this working example, we establish an SQL table which contains the spreadsheets we'd like to have generated. Each row in the table includes the query that will supply the data for the spreadsheet, and the location in the IFS where the spreadsheet should reside. Further, a different procedure emails the spreadsheets to an interested consumer. …
-- =================================================================
-- Author: Scott Forstie
-- Email : forstie@us.ibm.com
-- Date : January 10, 2020
-- =================================================================
--
-- Setup:
-- 1) create and populate the spreadsheet generator table
-- 2) change the procedure source code:
@forstie
forstie / Use ACS on your IBM i to build spreadsheets.sql
Created January 17, 2020 17:02
This example simplifies a previous gist. ACS is now being shipped on your IBM i via PTFs. Subsequent PTFs will ship when major enhancements are made to ACS.
--
-- Now that ACS is shipped on the IBM i via PTFs, we no longer need to manually move the
-- acsbundle.jar onto the IBM i. Just apply the latest ACS PTFs and reference the jar
-- at: /QIBM/proddata/Access/ACS/Base/acsbundle.jar
--
-- Initial PTFs of ACS
-- V7R4M0 SI71900
-- V7R3M0 SI71934
--
@forstie
forstie / Extracting the IFS filename from an absolute path name.sql
Created September 28, 2021 01:55
The request was, if you have an absolute path, how can SQL extract the filename from the path? One approach is found below.
--
-- Subject: Extracting the IFS file name from a path, using regular expression built-in functions
-- Author: Scott Forstie
-- Date : September 27, 2021
-- Features Used : This Gist uses regexp_count, regexp_instr, substr, and SQL PL
--
-- Function - The request was, if you have an absolute path, how can SQL extract the filename from the path?
-- One approach is found below.
--
--
@forstie
forstie / Compare the contents of two spooled files.sql
Created December 10, 2021 17:29
The challenge was simple... can SQL be used to compare the contents of two spooled files? The solution follows...
-- ========================================================================
--
-- Subject: Comparing two spooled files
-- Author: Scott Forstie
-- Date : December, 2021
--
-- IBM i Services used : OUTPUT_QUEUE_ENTRIES_BASIC, SYSTOOLS.SPOOLED_FILE_DATA
-- SQL language features used: CTEs, Exception joins, Union, and row_number()
-- ========================================================================
@forstie
forstie / Audit Journal Management.sql
Last active May 2, 2024 07:53
I've been getting asked lots of good questions about how to configure and monitor the Audit Journal. Guess what? #SQLcandoit
--
-- Subject: Audit Journal Management
-- Author: Scott Forstie
-- Date : March, 2023
-- Features Used : This Gist uses qsys2.security_info, qsys2.journal_info, qsys2.object_statistics, qsys2.qcmdexc, CTEs, sysibmadm.env_sys_info, and SYSTOOLS.split
--
-- Notes:
-- ===============================================
-- 1) There are many configuration options to consider using when establishing the Audit Journal, this Gist uses SQL to examine some of the most important choices.
-- 2) Its important to have a retention strategy for audit journal - journal receivers
@forstie
forstie / db2_to_json.sql
Last active May 2, 2024 07:42
The request... Is it possible to extract data from IBM i into JSON format with a Db2 service?
--
-- Subject: The request... return SQL services detail using JSON.
-- Author: Scott Forstie
-- Date : April, 2023
-- Features Used : This Gist uses qsys2.syscolumns2, listagg(), rtrim(), dynamic SQL, SQL PL, PIPE
--
-- Note:
-- When someone asks you to return Db2 for i data "as JSON", they probably want you
-- to publish a JSON document, which contains good key names and of course, the data.
--
@forstie
forstie / Stay Current.sql
Created April 9, 2024 14:53
The age old problem is this... IBM recommends that IBM i clients stay up to date on software updates... but how does a client get current and stay current? This confluence of technologies highlights one approach to consider.
--
-- Subject: Stay Current
-- Author: Scott Forstie
-- Date : April, 2024
-- Features Used : This Gist uses SQL PL, systools.group_ptf_currency, sysibmadm.env_sys_info, SYSTOOLS.GENERATE_SPREADSHEET, SYSTOOLS.SEND_EMAIL
--
-- The age old problem is this... IBM recommends that IBM i clients stay up to date
-- on software updates... but how does a client get current and stay current?
--
-- This confluence of technologies highlights one approach to consider.