当前位置: 代码迷 >> DB2 >> DB2 日期函数 有关问题
  详细解决方案

DB2 日期函数 有关问题

热度:10231   发布时间:2013-02-26 00:00:00.0
DB2 日期函数 问题
表 test
   
  字段 id getdate
  类型 varchar(10) date
  值1 1 2003-05-01
  值2 2 2006-07-11


SQL : select 结果1 , 结果2 from test where id='1'

当id=1时

我想要的结果1,结果2分别 是 日期getdate 上一年度 就是 2002-01-01 和 2002-12-31 

如果等于2时 结果1 结果2 的 值 是 2005-01-01 和 2005-12-31 

也就是要 getdate 字段的上一年度出时间和上一年度末的时间

求高手帮忙谢谢

------解决方案--------------------------------------------------------
上季度末:
select case when month(getdate) <= 3 then trim(char(year(getdate -1 years)))||'-12-31'
when month(getdate) <= 6 then trim(char(year(getdate)))||'-03-31'
when month(getdate) <= 9 then trim(char(year(getdate)))||'-06-30'
else trim(char(year(getdate)))||'-09-30' end
from test
上年末:
select trim(char(year(getdate -1 years)))||'-12-31'
from test
  相关解决方案