1 概述
节假日 是国务院每年维护的,所以需要特殊判断
2 示例
2.1 判断双休日
-- 2022 年全年: 364 天
select date '2022-12-31' - date '2022-01-01' 天数
from dual;
-- 系统默认 和 日常使用习惯
select to_char(sysdate, 'D') 默认天,
to_char(sysdate - 1, 'D') 习惯天
from dual;
-- 核心逻辑
select date '2022-01-01' + rownum - 1 日期,
to_char(date '2022-01-01' + rownum - 2, 'D') 星期几
from dual
connect by rownum <= 364;
2.2 判断节假日
with t_date_base as
( -- 一般日
select date '2022-01-01' + rownum - 1 日期,
to_char(date '2022-01-01' + rownum - 2, 'D') 星期几
from dual
connect by rownum <= 364),
t_date_jjr as
( -- 节假日
select date '2022-01-29' 日期, '补班' 日期类型 from dual
union all
select date '2022-01-30' 日期, '补班' 日期类型 from dual
union all
select date '2022-01-31' 日期, '除夕' 日期类型 from dual
union all
select date '2022-02-01' 日期, '春节' 日期类型 from dual)
select t1.日期,
t1.星期几,
(case -- 注意顺序
when t2.日期 is not null and t2.日期类型 <> '补班' then
'节假日'
when t2.日期 is not null and t2.日期类型 = '补班' then
'工作日'
when t1.星期几 in ('6', '7') then
'节假日' -- 双休日
else
'工作日'
end) 日期类型
from t_date_base t1,
t_date_jjr t2
where t2.日期(+) = t1.日期
order by t1.日期;
查询结果:(部分)
日期 星期几 日期类型
2022/1/24 1 工作日
2022/1/25 2 工作日
2022/1/26 3 工作日
2022/1/27 4 工作日
2022/1/28 5 工作日
2022/1/29 6 工作日
2022/1/30 7 工作日
2022/1/31 1 节假日
2022/2/1 2 节假日
3 扩展
3.1 Python 输出节假日
import datetime
import chinese_calendar
def get_holidays(year=None, include_weekends=True):
"""
获取某一年的所有节假日,默认当年
:param year: which year
:param include_weekends: False for excluding Saturdays and Sundays
:return: list
"""
# 若 year 为空,则默认当年
if not year:
year = datetime.datetime.now().year
# 开始时间
start = datetime.date(year, 1, 1)
# 结束时间
end = datetime.date(year, 12, 31)
# 假节日
holidays = chinese_calendar.get_holidays(start, end, include_weekends)
return holidays
if __name__ == '__main__':
date_test = get_holidays()
for day in date_test:
print(day)
3.2 遍历月份
select add_months(to_date('2023-01', 'YYYY-MM'), level - 1) as month, level
from dual
connect by level <=
months_between(to_date('2023-11', 'YYYY-MM'),
to_date('2023-01', 'YYYY-MM')) + 1;