一年前在db2中获取日期

时间:2013-10-30 16:08:55

标签: date db2

我在db2中有这样的日期:1950-01-31。我希望得到与一年前相符的日期(即1949-01-31)。 我从一个提示:

current date + 1 YEAR 
current date + 3 YEARS + 2 MONTHS + 15 DAYS 
current time + 5 HOURS - 3 MINUTES + 10 SECONDS

我试试:

select dateCol-1 year FROM table, 

但它给了我1950-01-30。  看起来它总是从当天而不是一年中扣除1。 我怎么能在一年前得到这个日期。

1 个答案:

答案 0 :(得分:0)

select date(days(dateCol)-(case when month(dateCol)=2 and day(dateCol)=29 then 366 else 365 end)) from table


db2 => values (date(days('28.02.2004')-(case when month(dateCol)=2 and day(dateCol)=29 then 366 else 365 end)))
>  2003-02-28
1 record(s) selected.

db2 => values (date(days('29.02.2004')-(case when month(dateCol)=2 and day(dateCol)=29 then 366 else 365 end)))
>  2003-02-28
1 record(s) selected.