DB2上一個季月SQL CREATE FUNCTION "DB2INST1"."QTR" www.2cto.com ("DDATE" DATE ) RETURNS CHARACTER(7) SPECIFIC "DB2INST1"."SQL130225092927300" LANGUAGE SQL NOT DETERMINISTIC READS SQL DATA STATIC DISPATCH CALLED ON NULL INPUT EXTERNAL ACTION INHERIT SPECIAL REGISTERS BEGIN ATOMIC DECLARE v CHARACTER(7) default ''; --declare cursor1 cursor for set v= ( select CASE WHEN TO_CHAR(DDATE,'Q') ='1' THEN to_char(year(DDATE)-1)||'-12' when TO_CHAR(DDATE,'Q') ='2' THEN to_char(year(DDATE))||'-03' when TO_CHAR(DDATE,'Q') ='3' THEN to_char(year(DDATE))||'-06' when TO_CHAR(DDATE,'Q') ='4' THEN to_char(year(DDATE))||'-09' END --into :v -- 上個季月 能夠知道跨年 from sysibm.sysdummy1 fetch first 1 rows only ) ; --fetch first 1 rows only --set sum = a + b; return v; END;