如何在Oracle SQL中使用日期格式进行计算

时间:2019-02-26 06:48:52

标签: sql oracle

感谢您对以下问题的帮助,我在这个问题上苦苦挣扎很长时间。请看下面的例子。

(SELECT MIN(CASE WHEN SI.DATE_STAT >= 78000000000000000 AND 
            SUBSTR(DIGITS(SI.DATE_STAT),5,2) BETWEEN '01' AND '12' AND 
            SUBSTR(DIGITS(SI.DATE_STAT),9,1) BETWEEN '0' AND '3' AND 
            INT(SUBSTR(DIGITS(SI.DATE_STAT),7,2)) BETWEEN 1 AND 
            DAY(DATE(CAST(19+INT(SI.DATE_STAT / 100000000000000000) AS CHAR(2)) || 
            TRANSLATE('YE-MN-01', DIGITS(SI.DATE_STAT), 'xxYEMNDA')) + 1 MONTH - 1 DAY)
            THEN TIMESTAMP(CAST(19+INT(SI.DATE_STAT / 100000000000000000) AS CHAR(2)) || 
            TRANSLATE('YE-MN-DA HR:UT:SC', DIGITS(SI.DATE_STAT),'xxYEMNDAxHRUTSC')) - 
            INT(MOD(3 - INT(SUBSTR(DIGITS(SI.DATE_STAT),9,1)),3)) DAYS + 
            INT(MOD(INT(SUBSTR(DIGITS(SI.DATE_STAT),16,2)) - 1,25) + 1) HOURS + 
            INT(MOD(4 - ((INT(SUBSTR(DIGITS(SI.DATE_STAT),16,2)) - 1) / 25),4) * 15) 
            MINUTES ELSE CAST(NULL AS TIMESTAMP)END) 
        FROM EXPORT.STAT_ITEM SI
        WHERE (SI.INVOICE_REF = SH.SHPMNT_REF) AND SI.STAT_CODE = 'ABC') AS ABC,

         (SELECT MIN(CASE WHEN SI.DATE_STAT >= 78000000000000000 AND 
            SUBSTR(DIGITS(SI.DATE_STAT),5,2) BETWEEN '01' AND '12' AND 
            SUBSTR(DIGITS(SI.DATE_STAT),9,1) BETWEEN '0' AND '3' AND 
            INT(SUBSTR(DIGITS(SI.DATE_STAT),7,2)) BETWEEN 1 AND 
            DAY(DATE(CAST(19+INT(SI.DATE_STAT / 100000000000000000) AS CHAR(2)) || 
            TRANSLATE('YE-MN-01', DIGITS(SI.DATE_STAT), 'xxYEMNDA')) + 1 MONTH - 1 DAY)
            THEN TIMESTAMP(CAST(19+INT(SI.DATE_STAT / 100000000000000000) AS CHAR(2)) || 
            TRANSLATE('YE-MN-DA HR:UT:SC', DIGITS(SI.DATE_STAT),'xxYEMNDAxHRUTSC')) - 
            INT(MOD(3 - INT(SUBSTR(DIGITS(SI.DATE_STAT),9,1)),3)) DAYS + 
            INT(MOD(INT(SUBSTR(DIGITS(SI.DATE_STAT),16,2)) - 1,25) + 1) HOURS + 
            INT(MOD(4 - ((INT(SUBSTR(DIGITS(SI.DATE_STAT),16,2)) - 1) / 25),4) * 15) 
            MINUTES ELSE CAST(NULL AS TIMESTAMP)END) 
        FROM EXPORT.STAT_ITEM SI
        WHERE (SI.INVOICE_REF = SH.SHPMNT_REF) AND SI.STAT_CODE = 'DEF') AS DEF,

//This should be wrong and I'm getting error.
coalesce((days(DEF) -  days(ABC)),'0') as TotalTransitTime

我要显示的是“ DEF”和“ ABC”之间的日期,如果缺少DEF或ABC,则默认值为0。请帮忙。非常感谢!

0 个答案:

没有答案