当前位置:数据库 > SQLServer >>

DB2上一个季月SQL

DB2上一个季月SQL
 
CREATE FUNCTION "DB2INST1"."QTR"   www.zzzyk.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;
 
 
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,