zoukankan      html  css  js  c++  java
  • Oracle 日期操作——年/月/日/星期/周/季度

    在表day_master_temp中插入未来一年的日期数据,留有盈余两个月(14个月)

    insert into day_master_temp(date1)(select date_dt from 
    (select add_months(trunc(current_timestamp),14)-level as date_dt
    from dual
    connect by level < add_months(trunc(current_timestamp),14)-trunc(current_timestamp)
    order by date_dt ))
    --current_timestamp取当前系统时间戳
    --trunc(current_timestamp)取到当天零时
    --connect by 递归调用
    --level递归的层
    --add_months日期函数,加n个月后是哪天 

    在表day_master_temp中更新未来一年日期数据对应的年、月、日、季度、星期、周(以不同的星期算一周)

    ---year1
    update day_master_temp t set t.year1=to_char(date1,'yyyy')
    ---year_month
    update day_master_temp t set t.year_month=to_char(date1,'yyyy-mm')
    ---month_num
    update day_master_temp t set t.month_num=to_number(to_char(date1,'mm'))
    ---day_num
    update day_master_temp t set t.day_num=to_number(to_char(date1,'dd'))
    ---xingqi
    update day_master_temp t set t.xingqi=to_char(date1,'day')
    ---flag 星期四
    update day_master_temp t set t.flag='week_last_day' where t.xingqi='星期四'
    ---week_num1 week_num  iw表示一年中的第几周
    update day_master_temp t set t.week_num1=to_char(t.date1+2,'iw') 
    update day_master_temp t set t.week_num=to_char(t.date1+2,'iw') 
    ---week_num1 year_week  
    update day_master_temp t set t.year_week=to_char(t.date1,'yyyy') || '-' ||to_char(t.date1+2,'iw')  
    ---week_num1 year_week   需处理年头年尾 
    update day_master_temp t set t.year_week=to_char(t.date1,'yyyy') || '-' ||to_char(t.date1+2,'iw')
    ---week_original 周一开始
    update day_master_temp t set t.week_original=to_char(t.date1,'iw')
    ---week_report 周五开始
    update day_master_temp t set t.week_report=to_char(t.date1+3,'iw') 
    ---week2 周六开始
    update day_master_temp t set t.week2=to_char(t.date1+2,'iw') 
    ---quarter 季度
    update day_master_temp t set t.quarter=to_char(t.date1,'q') 

    Oracle数据库中周iw、ww、w的区别

    ww:Same day of the week as the first day of the year——按年度1月1日的第一天为每周第一天

    iw:Same day of the week as the first day of the ISO week, which is Monday—— 每周一

    w:Same day of the week as the first day of the month——按月份1日的第一天作为每周第一天

    select
    --oracle求当年的第几周,从当年的第一个星期一算当年第一周的第一天 
    to_char(TO_DATE('20110101', 'YYYYMMDD'), 'yyyyiw') as week, 
    --oracle求当年的第几周,从当年的1月1日算一年的第一周的第一天
    to_char(TO_DATE('20110101', 'YYYYMMDD'), 'yyyyww') as week2,
    --oracle求当月的第几周,从当月的1号算一周的第一天 
    to_char(TO_DATE('20110201', 'YYYYMMDD'), 'yyyyw') as week3,
    --oracle求第几年
    to_char(TO_DATE('20110101', 'YYYYMMDD'), 'yyyy') as year, 
    --oracle求当年的第几月
    to_char(TO_DATE('20110101', 'YYYYMMDD'), 'yyyymm') as month, 
    --oracle求当年的第几天
    to_char(TO_DATE('20110101', 'YYYYMMDD'), 'yyyyddd') as day, 
    --oracle求当年的第几季度
    to_char(TO_DATE('20110401', 'YYYYMMDD'), 'yyyyq') as quarter 
    from dual

     WEEK    WEEK2    WEEK3    YEAR    MONTH    DAY    QUARTER
     201152    201101    20111    2011    201101    2011001    20112

  • 相关阅读:
    把数据库转化成数据库脚本
    营养瘦身第一菜——金陵素什锦 健康程序员,至尚生活!
    十类好吃不胖的食物 健康程序员,至尚生活!
    一周带饭实录(附作菜菜谱) 健康程序员,至尚生活!
    日常五大习惯有助减肥 健康程序员,至尚生活!
    【暴强】200种好口碑便宜护肤品 健康程序员,至尚生活!
    腹式肠道操 缩胃瘦身有奇效 健康程序员,至尚生活!
    一天4时段喝水轻松瘦身 健康程序员,至尚生活!
    10种不要钱的护肤法则 健康程序员,至尚生活!
    看了这篇你肯定瘦 全身上下想瘦哪就瘦哪 健康程序员,至尚生活!
  • 原文地址:https://www.cnblogs.com/bellin124/p/14217219.html
Copyright © 2011-2022 走看看