资讯专栏INFORMATION COLUMN

oracle下sql创建指定年份全年日期表(区分工作日)

whidy / 456人阅读

摘要:作者的话当系统执行到本处时,将本年度或指定年份一整年天对应的是否为工作日情况数据插入到指定表中如表示工作日,表示双休日,法定节假日手动调整。

作者的话:
当系统执行到本处sql时,将本年度(或指定年份)一整年365天对应的是否为工作日情况数据插入到指定表中(如:0表示工作日,1表示双休日,法定节假日手动调整)。
1.创建表:
create table WORK_DAYS
(
  work_days_id NUMBER not null,
  one_day      DATE,
  type         NUMBER,
  created_on   DATE,
  created_by   NUMBER,
  updated_on   DATE,
  updated_by   NUMBER
)
tablespace USERS
  pctfree 10
  initrans 1
  maxtrans 255
  storage
  (
    initial 64K
    next 1M
    minextents 1
    maxextents unlimited
  );
2.oracle表主键自增序列:
create sequence seq_work_days
increment by 1 
start with 1 
nomaxvalue 
nocycle 
nocache; 
3.查出结果并插入至指定表:
insert into work_days(work_days_id,one_day,type)
with x0 as (select to_date("2016-01-01","yyyy-MM-dd") as 年初,to_date("2016-12-31","yyyy-MM-dd") as 年末 from dual ),
x1 as (select 年初 + level - 1 as 日期  from x0 connect by level <= (年末 - 年初) + 1),
x2 as (select 日期,to_number(to_char(日期, "d")) 周几 from x1)
select seq_work_days.nextval,日期,(case when 周几=1 or 周几=7 then then 1 else 0 end) as 工作日标志 from x2

【Select to_char(sysdate,"d") from dual取当前时间是一周的第几天,从星期天开始,周六结束。即1为星期日,以此类推。】

4.查看结果:
select t.*, t.rowid from WORK_DAYS t

5.效果图:

阅读需要支付1元查看
<