0

在我的表中,我有两个单独的日期字段 X 和 Y。在字段 x 中我的日期是 13/08/2008,在字段 YI 中的日期是 13/08/2015。

我想知道是否有一种 sql 方法可以计算出字段 X 和 Y 之间的六个月日期周期,并将结果显示在单独的行上?所以,我希望在两个领域有以下结果:

BEGIN     | END
13-Aug-08 | 13-Feb-09
13-Feb-09 | 13-Aug-09
13-Aug-09 | 15-Feb-10
15-Feb-10 | 13-Aug-10
13-Aug-10 | 14-Feb-11
14-Feb-11 | 15-Aug-11
15-Aug-11 | 13-Feb-12
13-Feb-12 | 13-Aug-12
13-Aug-12 | 13-Feb-13
13-Feb-13 | 13-Aug-13
13-Aug-13 | 13-Feb-14
13-Feb-14 | 13-Aug-14
13-Aug-14 | 13-Feb-15
13-Feb-15 | 13-Aug-15

这可能吗?

谢谢

4

1 回答 1

0

你可以尝试这样的事情:

DATE '2006-05-20' + INTERVAL 5 DAY   -> DATE '2006-05-25' 
DATE '2006-05-20' + INTERVAL 5 MONTH -> DATE '2006-10-20' 
DATE '2006-05-20' + INTERVAL 5 YEAR  -> DATE '2011-05-20' 

示例 1:

CREATE TABLE BEGIN_DATES
(
     BDates DATETIME
); 

INSERT INTO BEGIN_DATES VALUE ('13/08/2008'); 
INSERT INTO BEGIN_DATES VALUE ('13/02/2009');
INSERT INTO BEGIN_DATES VALUE ('13/08/2009');
INSERT INTO BEGIN_DATES VALUE ('15/02/2010');
INSERT INTO BEGIN_DATES VALUE ('13/08/2010');
INSERT INTO BEGIN_DATES VALUE ('14/02/2011');
INSERT INTO BEGIN_DATES VALUE ('15/08/2011');
INSERT INTO BEGIN_DATES VALUE ('13/02/2012');
INSERT INTO BEGIN_DATES VALUE ('13/08/2012');
INSERT INTO BEGIN_DATES VALUE ('13/02/2013');
INSERT INTO BEGIN_DATES VALUE ('13/08/2013');
INSERT INTO BEGIN_DATES VALUE ('13/02/2014');
INSERT INTO BEGIN_DATES VALUE ('13/08/2014');
INSERT INTO BEGIN_DATES VALUE ('13/02/2015');


SELECT BDates AS BEGIN, (BDates + INTERVAL 6 MONTH) AS END
FROM BEGIN_DATES

DROP TABLE BEGIN_DATES

示例 2:

BEGIN
SET @date = '2008-08-13';
SET @Edate ='2015-08-13';

WHILE @date <= @Edate DO

SELECT @date AS BEGIN_DATE, CAST(@date AS DATETIME) + INTERVAL 6 MONTH AS END_DATE;  -- ADDDATE(@date,INTERVAL 6 MONTH)

SET @date = CAST(@date AS DATETIME) + INTERVAL 6 MONTH;   -- Or ADDDATE(@date,INTERVAL 6 MONTH)

END WHILE;
END
于 2012-05-23T15:52:56.493 回答