Informix SQL - 2 个日期之间的天数

Informix SQL - Number of days between 2 dates

我试图获取 2 个日期时间之间的天数(时间戳值为 00:00:00)并尝试了以下查询:

select
   "Date1",
   "Date2",
   extend("Date1", year to fraction(5)) - extend("Date2", year to fraction(5)) as "TotalDaysDifference" 
from
   "mytable"

此查询 returns 值的方式如下:

我只需要其中的 788144100 值。我该如何从这个间隔中提取出来?

您使用的是哪个版本的 Informix?

这是一个使用 Informix 14.10.FC5 的例子:

CREATE TABLE mytable
(
  id    INTEGER,
  date1 DATETIME YEAR TO SECOND,
  date2 DATETIME YEAR TO SECOND
);
-- Using 2021-02-28 instead of 2021-02-29 because 2021-02-29 is an incorrect date and Informix returns an error.
INSERT INTO mytable VALUES ( 1, '2021-02-28 00:00:00', '2021-09-27 00:00:00' );
INSERT INTO mytable VALUES ( 2, '1900-12-31 00:00:00', '2021-09-27 00:00:00' );

SELECT
  date1,
  date2,
  ( date1 - date2 )::INTERVAL DAY(9) TO DAY AS daysdifference1, -- no need to transform the datetime with "extend". I just cast the result to an interval of days
  ( date2 - date1 )::INTERVAL DAY(9) TO DAY AS daysdifference2
FROM
  mytable
WHERE
  id = 1
;

date1               date2               daysdifference1 daysdifference2

2021-02-28 00:00:00 2021-09-27 00:00:00       -211             211

SELECT
  date1,
  date2,
  ( date1 - date2 )::INTERVAL DAY(9) TO DAY AS daysdifference1, -- no need to transform the datetime with "extend". I just cast the result to an interval of days
  ( date2 - date1 )::INTERVAL DAY(9) TO DAY AS daysdifference2
FROM
  mytable
WHERE
  id = 2
;

date1               date2               daysdifference1 daysdifference2

1900-12-31 00:00:00 2021-09-27 00:00:00     -44100           44100

如果您不关心两个 date/time 值中的时间,则只需转换为 DATE 值并减去:

SELECT "Date1", "Date2",
       "Date1"::DATE, "Date2"::DATE,
       "Date2"::DATE - "Date1"::DATE
  FROM "mytable"

您必须 运行 DELIMIDENT 在环境中设置以启用用双引号括起来的定界标识符。

转换为DATE的好处是减法的结果是一个整数值,而不是一个区间,通常使用起来更简单。

你的第一个例子是:

If Day1 = '2021-02-29 00:00:00' and Day2 = '2021-09-27 00:00:00', then TotalDaysDifference = '7881 00:00:00.00000'.

根据您的 SQL,该值应为 Date1。由于 2021 年不是闰年,因此您不应该能够插入值 2021-02-29。此外,一年只有 365 天,因此不清楚为什么您期望同一年的两个日期之间有 7,881 天。

但是,如果我们把2021-02-29改成2000-02-29(2000年是闰年!),那么减法的结果确实是7881。

概念验证

CREATE TEMP TABLE "mytable"
(
    "Date1" DATETIME YEAR TO SECOND NOT NULL,
    "Date2" DATETIME YEAR TO SECOND NOT NULL
);

INSERT INTO "mytable" VALUES('1900-12-31 00:00:00', '2021-09-27 00:00:00');
--INSERT INTO "mytable" VALUES('2021-02-29 00:00:00', '2021-09-27 00:00:00');
INSERT INTO "mytable" VALUES('2000-02-29 00:00:00', '2021-09-27 00:00:00');

SELECT "Date1", "Date2",
       "Date1"::DATE AS "Date1 AS DATE",
       "Date2"::DATE AS "Date1 AS DATE",
       "Date2"::DATE - "Date1"::DATE AS "Difference in days"
  FROM "mytable";

输出

我在环境中使用 DBDATE=Y4MD-,因此 DATE 值的格式类似于 DATETIME 值。

Date1 Date2 Date1 AS DATE Date1 AS DATE Difference in days
DATETIME YEAR TO SECOND DATETIME YEAR TO SECOND DATE DATE INTEGER
1900-12-31 00:00:00 2021-09-27 00:00:00 1900-12-31 2021-09-27 44100
2000-02-29 00:00:00 2021-09-27 00:00:00 2000-02-29 2021-09-27 7881