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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM