我在informix 11.5中遇到将时间范围分成几天的问题。我的要求是,我将获得两个日期,例如06/05/2015 11:00:00至06/07/2015 03:30:00。 我必须在存储过程中将这些时间范围拆分为以下内容,
我是informix中存储过程的新手。非常感谢您的帮助。
答案 0 :(得分:1)
有多种方法可以执行此操作,具体取决于您希望如何使用这些值。这是一种可行的方法:
CREATE PROCEDURE split_date_range(t0 DATETIME YEAR TO SECOND,
t1 DATETIME YEAR TO SECOND)
RETURNING DATETIME YEAR TO SECOND AS t_begin,
DATETIME YEAR TO SECOND AS t_end;
DEFINE tb DATETIME YEAR TO SECOND;
DEFINE te DATETIME YEAR TO SECOND;
DEFINE d1 DATE;
DEFINE de DATE;
DEFINE msg VARCHAR(72);
IF t0 IS NULL OR t1 IS NULL THEN
RAISE EXCEPTION -746, 0, "NULL value passed where not permitted";
END IF;
IF t0 > t1 THEN
LET msg = "t0 = " || t0 || " should not be bigger than t1 " || t1;
RAISE EXCEPTION -746, 0, msg;
END IF;
IF EXTEND(t0, YEAR TO DAY) = EXTEND(t1, YEAR TO DAY) THEN
RETURN t0, t1;
ELSE
LET te = EXTEND(EXTEND(t0, YEAR TO DAY), YEAR TO SECOND) +
INTERVAL(23:59:59) HOUR TO SECOND;
RETURN t0, te WITH RESUME;
LET d1 = DATE(EXTEND(t0, YEAR TO DAY)) + 1;
LET de = DATE(EXTEND(t1, YEAR TO DAY));
WHILE d1 < de
LET tb = EXTEND(d1, YEAR TO SECOND);
LET te = tb + INTERVAL(23:59:59) HOUR TO SECOND;
RETURN tb, te WITH RESUME;
LET d1 = d1 + 1;
END WHILE;
LET tb = EXTEND(d1, YEAR TO DAY);
RETURN tb, t1;
END IF;
END PROCEDURE;
显示的基本逻辑是:
然后你可以使用类似的东西:
EXECUTE PROCEDURE split_date_range(DATETIME(2015-06-05 11:00:00) YEAR TO SECOND,
DATETIME(2015-06-07 03:30:00) YEAR TO SECOND));
生成:
t_begin t_end
2015-06-05 11:00:00 2015-06-05 23:59:59
2015-06-06 00:00:00 2015-06-06 23:59:59
2015-06-07 00:00:00 2015-06-07 03:30:00