如何在考虑 UTC 的情况下格式化 Db2 时间戳

问题描述 投票:0回答:1

给出以下 DB2 脚本:

WITH ranked_dataset AS (
    SELECT 
        D.INSTNB AS Id,
        D.INSTNB AS transferId,
        H.FRSTCD AS fromStoreId,
        H.TOSTCD AS toStoreId,
        D.SENDDT AS sendDate,
        D.STANTS AS time_stamp,
        CASE 
            WHEN C.INSTNB IS NOT NULL THEN 'RECIEVED'
            ELSE 'SENT'
        END AS direction,
        json_arrayagg(
            json_object(
                'plu' value D.PRODCD,
                'quantity' value D.TRANQT,
                'sentProcessedTimestamp' value D.STANTS                 
            )
        ) items,
        ROW_NUMBER() OVER (PARTITION BY D.INSTNB, H.DIRECT, H.FRSTCD, H.TOSTCD, D.SENDDT ORDER BY D.STANTS DESC) AS rn
        
    FROM 
        SBOSFILES.HUBISTDT D
    INNER JOIN 
        SBOSFILES.HUBISTHD H
    ON 
        D.INSTNB = H.INSTNB
        AND D.FRSTCD = H.FRSTCD
        AND D.TOSTCD = H.TOSTCD
    LEFT JOIN
        SBOPFILES.HUBISTC C
    ON
        D.INSTNB = C.INSTNB
    WHERE
        H.DIRECT = 'T'
        AND DATE(D.STANTS) = '2018-04-30'
    GROUP BY 
        D.INSTNB,
        H.DIRECT,
        H.FRSTCD,
        H.TOSTCD,
        D.SENDDT,
        D.STANTS,
        C.INSTNB
)
SELECT json_object(
    'id' value (Id || '-' || direction),
    'transferId' value transferId,
    'fromStoreId' value fromStoreId,
    'toStoreId' value toStoreId,
    'sendDate' value sendDate,
    'time_stamp' value time_stamp,
    'direction' value direction,
    'items' value items FORMAT JSON
    )
FROM ranked_dataset  
WHERE rn = 1          
LIMIT 100

当前产生如下输出:

{"id":"1-SENT","transferId":1,"fromStoreId":2,"toStoreId":3,"sendDate":"2018-04-30","time_stamp":"2018-04-30-06.55.47.349000","direction":"SENT","items":[{"plu":25,"quantity":10,"sentProcessedTimestamp":"2018-04-30-06.55.47.349000"}]}

我需要进行更改,以便父对象“time_stamp”属性和子对象“sentProcessedTimestamp”属性采用以下格式:

"timestamp": "2018-04-30T05:55:47.349+01:00"

我尝试过这样做:

TO_CHAR(D.STANTS, 'YYYY-MM-DD"T"HH24:MI:SS.FF3') || '+01:00' AS time_stamp,  

但是我收到以下错误:

处理结果时发生错误。 - 不格式化字符串 对于函数 VARCHAR_FORMAT 有效。 SQLCODE=-20447,SQLSTATE=22007, 驱动程序=4.19.56

更多背景

此查询将从 Azure 数据工厂管道运行,并将发送到 blob 存储容器,azure 函数将拾取该容器并将其插入到 mongo db 中。我们希望插入 mongo 的所有内容都是 UTC。

sql db2
1个回答
0
投票

不理想,但这是我想到的最好的:

with xx as (
  select CURRENT TIMESTAMP as ats FROM sysibm.sysdummy1
)
SELECT replace(to_char(ats, 'YYYY-MM-DD HH24:MI:SS.FF3'),' ' ,'T')  from xx
© www.soinside.com 2019 - 2024. All rights reserved.