Question

Using DB2 on the mainframe, I have a value indicating number of microseconds that a job took, say 26,366,861,945.

Is there any easy way to get that into the H:MM:SS.mmmmmm format using standard DB2 functions? In other words, that value above would become 7:19:26.861945.

If necessary, we can drop the microseconds and accept HH:MM:SS.

If it's over 24 hours, I'm not fussed about showing days, 27:02:14 would be fine.

I though about selecting the zero time plus xx microseconds but couldn't get it to work.

Was it helpful?

Solution

Well, it's absolutely atrocious, but this should work:

WITH

ORIG (TS) AS (
    SELECT BIGINT(REPLACE('26,366,861,945', ',', ''))
    FROM SYSIBM.SYSDUMMY1
),

CONV (TS) AS (
    SELECT TIMESTAMP_ISO('0001-01-01') + (SELECT TS FROM ORIG) MICROSECOND
    FROM SYSIBM.SYSDUMMY1
),

FMT (TS) AS (
    SELECT VARCHAR_FORMAT(TS, 'DD-HH24:MI:SS.FF')
    FROM CONV
),

DAYLOC (D) AS (
    SELECT LOCATE('-', TS)-1
    FROM FMT
),

HOURLOC (H) AS (
    SELECT LOCATE(':', TS, (SELECT D FROM DAYLOC))-1
    FROM FMT
),

DY (D) AS (
    SELECT CAST(SUBSTR(TS, 1, LOCATE('-', TS)-1) AS INTEGER)-1
    FROM FMT
),

HOURS (H) AS (
    SELECT CAST(SUBSTR(TS, (SELECT D FROM DAYLOC)+2, 2) AS INTEGER)
    FROM FMT
)

SELECT 
    RTRIM(CHAR(((SELECT D FROM DY) * 24) + (SELECT H FROM HOURS))) ||
    SUBSTR(TS, (SELECT H FROM HOURLOC) +1)
FROM FMT

I split it into parts, so it should be fairly easy to follow. Convert the string to an integer, add that to a specific date (the date really doesn't matter, as long as it's the first of the month), then use some string searching / formatting to convert it to hours instead of days.

OTHER TIPS

Try something like this (I can not test this):

TIMESTAMP_ISO('0000-00-00-00:00:00.000000') + MICROSECOND(DB2_TABLE.DB2_COLUMN)

Time calculations DB2 Basics: Fun with Dates and Times Timestamp function TIMESTAMP_ISO

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top