Rediranch, here is something that may (or may not) interest you. This SQL will take a timestamp duration and display it in a timestamp like format (repeat, this is NOT a timestamp. It is just a character representation of the timestamp duration similar to a timestamp format).
Code:
WITH TS_TAB
AS(
SELECT '000' || CAST(YEAR (CURRENT_TIMESTAMP - (CURRENT_TIMESTAMP - 4 HOUR - 3 MINUTE - 2 SECOND - 1 MICROSECOND)) AS CHAR(4)) AS YYYY
, LENGTH(STRIP(CAST(YEAR (CURRENT_TIMESTAMP - (CURRENT_TIMESTAMP - 4 HOUR - 3 MINUTE - 2 SECOND - 1 MICROSECOND)) AS CHAR(4)))) AS YYYY_LEN
, '0' || CAST(MONTH (CURRENT_TIMESTAMP - (CURRENT_TIMESTAMP - 4 HOUR - 3 MINUTE - 2 SECOND - 1 MICROSECOND)) AS CHAR(2)) AS MM
, LENGTH(STRIP(CAST(MONTH (CURRENT_TIMESTAMP - (CURRENT_TIMESTAMP - 4 HOUR - 3 MINUTE - 2 SECOND - 1 MICROSECOND)) AS CHAR(2)))) AS MM_LEN
, '0' || CAST(DAY (CURRENT_TIMESTAMP - (CURRENT_TIMESTAMP - 4 HOUR - 3 MINUTE - 2 SECOND - 1 MICROSECOND)) AS CHAR(2)) AS DD
, LENGTH(STRIP(CAST(DAY (CURRENT_TIMESTAMP - (CURRENT_TIMESTAMP - 4 HOUR - 3 MINUTE - 2 SECOND - 1 MICROSECOND)) AS CHAR(2)))) AS DD_LEN
, '0' || CAST(HOUR (CURRENT_TIMESTAMP - (CURRENT_TIMESTAMP - 4 HOUR - 3 MINUTE - 2 SECOND - 1 MICROSECOND)) AS CHAR(2)) AS HH
, LENGTH(STRIP(CAST(HOUR (CURRENT_TIMESTAMP - (CURRENT_TIMESTAMP - 4 HOUR - 3 MINUTE - 2 SECOND - 1 MICROSECOND)) AS CHAR(2)))) AS HH_LEN
, '0' || CAST(MINUTE (CURRENT_TIMESTAMP - (CURRENT_TIMESTAMP - 4 HOUR - 3 MINUTE - 2 SECOND - 1 MICROSECOND)) AS CHAR(2)) AS MN
, LENGTH(STRIP(CAST(MINUTE (CURRENT_TIMESTAMP - (CURRENT_TIMESTAMP - 4 HOUR - 3 MINUTE - 2 SECOND - 1 MICROSECOND)) AS CHAR(2)))) AS MN_LEN
, '0' || CAST(SECOND (CURRENT_TIMESTAMP - (CURRENT_TIMESTAMP - 4 HOUR - 3 MINUTE - 2 SECOND - 1 MICROSECOND)) AS CHAR(2)) AS SS
, LENGTH(STRIP(CAST(SECOND (CURRENT_TIMESTAMP - (CURRENT_TIMESTAMP - 4 HOUR - 3 MINUTE - 2 SECOND - 1 MICROSECOND)) AS CHAR(2)))) AS SS_LEN
, '00000' || CAST(MICROSECOND(CURRENT_TIMESTAMP - (CURRENT_TIMESTAMP - 4 HOUR - 3 MINUTE - 2 SECOND - 1 MICROSECOND)) AS CHAR(6)) AS MS
, LENGTH(STRIP(CAST(MICROSECOND(CURRENT_TIMESTAMP - (CURRENT_TIMESTAMP - 4 HOUR - 3 MINUTE - 2 SECOND - 1 MICROSECOND)) AS CHAR(6)))) AS MS_LEN
FROM SYSIBM.SYSDUMMY1
)
SELECT SUBSTR(YYYY, YYYY_LEN, 4) || '-'
|| SUBSTR(MM , MM_LEN , 2) || '-'
|| SUBSTR(DD , DD_LEN , 2) || '-'
|| SUBSTR(HH , HH_LEN , 2) || '.'
|| SUBSTR(MN , MN_LEN , 2) || '.'
|| SUBSTR(SS , SS_LEN , 2) || '.'
|| SUBSTR(MS , MS_LEN , 6) AS TS_DUR_DISPLAY
FROM TS_TAB
TS_DUR_DISPLAY
--------------------------
0000-00-00-04.03.02.000001
I am using CURRENT_TIMESTAMP and subtracting durations to get a different timestamp for the substraction (otherwise, I would get all zeros in the output). You would put your second timestamp in place of this.
You could do this all in one very large statement and not use the Common Table Expression (CTE). I did this to make the process a little easier to see.
You could also put this (or something like it) in a function.
Basically, it subtracts the timestamps to get the timestamp duration.
Then it uses the various date/time functions to extract them out (the Year, Month, Day, etc.).
Since these results are Integers, I Cast them as Characters.
I also Concatenate enough leading zeros to ensure they are at least the length of the value.
I also run the same process (except for concatenating the zeros) with a STRIP function (to remove spaces) and the LEN function to get the actual lenght of the value (Year could be 1-4 characters, Microseconds could be 1-6 characters and all the rest could be 1-2 characters).
This gets the base values in the CTE TS_TAB.
Then it is a simple matter of using the SUBSTR function (with the Length value to get the correct starting position) and concatenate all the values together along with separators.
This results in a character string that represents the duration in a timestamp-like format.