前言
在IBMiSeries上仍有许多上个世纪的遗留应用程序在使用。日期字段通常以各种格式集体存储,通常称为Julian Date(儒略日期,JD)。这些格式是一年和一年中某一天的组合,更准确地说是今天的序数日期。
日期转换
- Julian Date转日历日期 ```sql Select date( days(concat(cast(integer(1900000 + “SDIVD”) / 1000 as Char(4)), ‘-01-01’)) + mod(integer(1900000 + “ SDIVD”), 1000) -1) FROM sysibm.sysdummy1;
— 2040-01-01 SELECT DATE(CHAR(140001 + 1900000)) FROM sysibm.sysdummy1;
— 对于IBM i Db2(=7.2版) — Julian to Db2 Date (Method 1.0) DATE(CAST(120050 + 1900000 AS CHAR(7)))
— 对于IBM i Db2(7.2版+) — Julian to Db2 Date (Method 1.1) DATE(CHAR(120050 + 1900000))
- **日历日期转Julian Date**
```sql
SELECT INT(TO_CHAR('2019-10-18','YYYYDDD'))-1900000 FROM sysibm.sysdummy1;
-- 对于IBM i Db2(=7.2版)
-- Db2 Date to Julian (Method 2.0)
1000 * (YEAR(DATE('02/19/2020')) - 1900) + DAYOFYEAR(DATE('02/19/2020'))
综合案例
SELECT
TEST_DATA."DATE"
, TEST_DATA."JULIAN"
, YEAR(TEST_DATA."DATE") AS "YEAR"
, DAYOFYEAR(TEST_DATA."DATE") AS "DAYOFYEAR"
, DATE(DAYS(CONCAT(CAST(INTEGER(1900000 + TEST_DATA."JULIAN") / 1000 AS CHAR(4)), '-01-01')) + MOD(INTEGER(1900000 + TEST_DATA."JULIAN"), 1000) - 1) AS "METHOD_IBM"
, DATE(CAST((TEST_DATA."JULIAN" + 1900000) AS CHAR(7))) AS "METHOD_1.0"
, DATE(CHAR(TEST_DATA."JULIAN" + 1900000)) AS "METHOD_1.1"
, 1000 * (YEAR(TEST_DATA."DATE") - 1900) + DAYOFYEAR(TEST_DATA."DATE") AS "METHOD_2.0"
FROM
TABLE
(
VALUES
(DATE('12/31/1938'), 039365)
, (DATE('12/31/1939'), 039365)
, (DATE('01/01/1940'), 040001)
, (DATE('02/19/2020'), 120050)
, (DATE('2020-01-01'), 119366)
, (DATE('2039-01-01'), 139001)
, (DATE('2039-12-31'), 139365)
, (DATE('2040-01-01'), 140001)
, (DATE('2041-01-15'), 141015)
)
AS TEST_DATA("DATE", "JULIAN")
;