SP_CREATE_TIME_DIMENSION 自动生成周末日期

本文介绍了一个用于生成时间维度数据的存储过程SP_CREATE_TIME_DIMENSION。该过程接受开始时间和结束时间作为参数,并在这两个时间范围内逐天生成时间维度数据,包括日期、年份、季度、月份、周数和星期等信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

CREATE OR REPLACE PROCEDURE SP_CREATE_TIME_DIMENSION(begin_date in varchar2,
                                                    end_date in varchar2) is


  /*SP_CREATE_TIME_DIMENSION: 生成时间维数据
  begin_date: 起始时间
  end_date:结束时间
  */
dDate date;
v_the_date number;
v_the_year varchar2(4);
v_the_quarter varchar2(2);
v_the_month varchar2(10);
v_the_month2 varchar2(2);
v_the_week varchar2(2);
v_the_day varchar2(10);
v_the_day2 varchar2(2);
v_week_day nvarchar2(10);


adddays int;
BEGIN
adddays := 1 ;
dDate := to_date(begin_date,'yyyymmdd');


WHILE (dDate <= to_date(end_date,'yyyymmdd'))
loop
   v_the_date := to_number(to_char(dDate,'yyyymmdd'));--key值
   v_the_year:= to_char(dDate, 'yyyy');--年
   v_the_quarter := to_char(dDate, 'q');--季度
   v_the_month:=to_char(dDate, 'mm');--月份(字符型)
   v_the_month2:=to_number(to_char(dDate, 'mm'));--月份(数字型)
   v_the_day:=to_char(dDate, 'dd');--日(字符型)
   v_the_day2:=to_char(dDate, 'dd');
   v_the_week:= to_char(dDate,'fmww');--年的第几周
   v_week_day := to_char(dDate, 'day'); --星期几




  insert into time_dimension(the_date,date_name,the_year,year_name,
                            the_quarter,quarter_name,the_month,
                            month_name,the_week,week_name,week_day)
                      values(v_the_date,v_the_year||'年'||v_the_month2||'月'||v_the_day2||'日',v_the_year,v_the_year||'年',
                            v_the_year||'Q'||v_the_quarter,v_the_year||'年'||v_the_quarter||'季度',to_number(v_the_year||v_the_month),
                            v_the_year||'年'||v_the_month2||'月',v_the_week,'第'||v_the_week||'周',
                            v_week_day);


   dDate :=  dDate + adddays;
END loop;
commit;
end SP_CREATE_TIME_DIMENSION;
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值