无法根据Informix中的日期过滤器过滤记录

时间:2016-09-22 10:12:41

标签: datetime informix

我想在Informix查询中添加过滤器:

WHERE agentstatedetail.eventdatetime < '1753-01-01 00:00:00' - INTERVAL(3) DAY TO DAY

但它失败了...... 请告诉它出错的地方。

1 个答案:

答案 0 :(得分:2)

如评论中所述,解决方案是确保将字符串解释为DATETIME值。这样做的简单方法是使用DATETIME文字符号:

DATETIME(1753-01-01 00:00:00) YEAR TO SECOND

演示:

CREATE TABLE agentstatedetail
(
    eventdatetime DATETIME YEAR TO SECOND NOT NULL PRIMARY KEY,
    eventname VARCHAR(64) NOT NULL
);

INSERT INTO agentstatedetail VALUES('1752-12-25 12:00:00', 'Christmas Day, Noon, 1752');
INSERT INTO agentstatedetail VALUES('1752-12-31 12:00:00', 'New Year''s Eve, Noon, 1752');
INSERT INTO agentstatedetail VALUES('1753-01-01 12:00:00', 'New Year''s Day, Noon, 1753');

SELECT * FROM agentstatedetail WHERE agentstatedetail.eventdatetime < '1753-01-01 00:00:00' - INTERVAL(3) DAY TO DAY;

这是嵌入最小SELECT语句的原始WHERE子句。它产生错误:

SQL -1261: Too many digits in the first field of datetime or interval.

(注意:在问题中包含错误信息会很有帮助。)

以下是查询的替代版本,其中包含DATETIME文字:

SELECT * FROM agentstatedetail
    WHERE agentstatedetail.eventdatetime < DATETIME(1753-01-01 00:00:00) YEAR TO SECOND -
                                           INTERVAL(3) DAY TO DAY
;

样本数据的输出:

1752-12-25 12:00:00|Christmas DAY, Noon, 1752

我观察到计算出的值是一个常数;你可以将代码重写为:

SELECT * FROM agentstatedetail
    WHERE agentstatedetail.eventdatetime < DATETIME(1752-12-29 00:00:00) YEAR TO SECOND

我怀疑该值是作为参数传递给该行的某个地方。

或者,您可以将字符串转换为DATETIME值,并获得相同的结果:

SELECT * FROM agentstatedetail
    WHERE agentstatedetail.eventdatetime < CAST('1753-01-01 00:00:00' AS DATETIME YEAR TO SECOND) -
                                           INTERVAL(3) DAY TO DAY
;

或:

SELECT * FROM agentstatedetail
    WHERE agentstatedetail.eventdatetime < '1753-01-01 00:00:00'::DATETIME YEAR TO SECOND -
                                           INTERVAL(3) DAY TO DAY