日期:2014-05-16  浏览次数:20497 次

DB2 sql 上期(月、季、年)上月月末,上季末,上年末(统计中常用到)


SELECT

current timestamp -day(current? date) days ,-- 上个月月末 ,能够知道跨年

?

/*

to_date('2013-02-23','yyyy-mm-dd')?-day(current? date) days ,-- 上个月月末 ,能够知道跨年

*/

?

CASE? WHEN? TO_CHAR('2012-06-22','Q') ='1' THEN? to_char(year('2012-12-01')-1)||'-12'
when? TO_CHAR('2012-06-22','Q') ='2' THEN to_char(year('2012-06-22'))||'-3'
when? TO_CHAR('2012-01-01','Q') ='3' THEN to_char(year('2012-12-01'))||'-6'
when? TO_CHAR('2012-01-01','Q') ='4' THEN to_char(year('2012-12-01'))||'-9'
?END AS tt , -- 上个季月 能够知道跨年


current date - (month(current date) -1 ) month - day(current date) day? --上年年末
?
?from sysibm.sysdummy1;?