前言

在IBMiSeries上仍有许多上个世纪的遗留应用程序在使用。日期字段通常以各种格式集体存储,通常称为Julian Date(儒略日期,JD)。这些格式是一年和一年中某一天的组合,更准确地说是今天的序数日期。
image.png

日期转换

  • 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))

  1. - **日历日期转Julian Date**
  2. ```sql
  3. SELECT INT(TO_CHAR('2019-10-18','YYYYDDD'))-1900000 FROM sysibm.sysdummy1;
  4. -- 对于IBM i Db2(=7.2版)
  5. -- Db2 Date to Julian (Method 2.0)
  6. 1000 * (YEAR(DATE('02/19/2020')) - 1900) + DAYOFYEAR(DATE('02/19/2020'))

综合案例

  1. SELECT
  2. TEST_DATA."DATE"
  3. , TEST_DATA."JULIAN"
  4. , YEAR(TEST_DATA."DATE") AS "YEAR"
  5. , DAYOFYEAR(TEST_DATA."DATE") AS "DAYOFYEAR"
  6. , 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"
  7. , DATE(CAST((TEST_DATA."JULIAN" + 1900000) AS CHAR(7))) AS "METHOD_1.0"
  8. , DATE(CHAR(TEST_DATA."JULIAN" + 1900000)) AS "METHOD_1.1"
  9. , 1000 * (YEAR(TEST_DATA."DATE") - 1900) + DAYOFYEAR(TEST_DATA."DATE") AS "METHOD_2.0"
  10. FROM
  11. TABLE
  12. (
  13. VALUES
  14. (DATE('12/31/1938'), 039365)
  15. , (DATE('12/31/1939'), 039365)
  16. , (DATE('01/01/1940'), 040001)
  17. , (DATE('02/19/2020'), 120050)
  18. , (DATE('2020-01-01'), 119366)
  19. , (DATE('2039-01-01'), 139001)
  20. , (DATE('2039-12-31'), 139365)
  21. , (DATE('2040-01-01'), 140001)
  22. , (DATE('2041-01-15'), 141015)
  23. )
  24. AS TEST_DATA("DATE", "JULIAN")
  25. ;