Oracle时间日期计算--计算某一日期为一年中的第几周
select
to_char(sysdate-10,'yyyymmdd')||':iw:'||to_char(sysdate-10,'iw')||':ww:'||to_char(sysdate-10,'ww')||':w:'||to_char(sysdate-10,'w')||':D周:'||to_char(sysdate-10,'D') d1,
to_char(sysdate-09,'yyyymmdd')||':iw:'||to_char(sysdate-09,'iw')||':ww:'||to_char(sysdate-09,'ww')||':w:'||to_char(sysdate-09,'w')||':D周:'||to_char(sysdate-09,'D') d2,
to_char(sysdate-08,'yyyymmdd')||':iw:'||to_char(sysdate-08,'iw')||':ww:'||to_char(sysdate-08,'ww')||':w:'||to_char(sysdate-08,'w')||':D周:'||to_char(sysdate-08,'D') d3,
to_char(sysdate-07,'yyyymmdd')||':iw:'||to_char(sysdate-07,'iw')||':ww:'||to_char(sysdate-07,'ww')||':w:'||to_char(sysdate-07,'w')||':D周:'||to_char(sysdate-07,'D') d4,
to_char(sysdate-06,'yyyymmdd')||':iw:'||to_char(sysdate-06,'iw')||':ww:'||to_char(sysdate-06,'ww')||':w:'||to_char(sysdate-06,'w')||':D周:'||to_char(sysdate-06,'D') d5,
to_char(sysdate-05,'yyyymmdd')||':iw:'||to_char(sysdate-05,'iw')||':ww:'||to_char(sysdate-05,'ww')||':w:'||to_char(sysdate-05,'w')||':D周:'||to_char(sysdate-05,'D') d6,
to_char(sysdate-04,'yyyymmdd')||':iw:'||to_char(sysdate-04,'iw')||':ww:'||to_char(sysdate-04,'ww')||':w:'||to_char(sysdate-04,'w')||':D周:'||to_char(sysdate-04,'D') d7,
to_char(sysdate-03,'yyyymmdd')||':iw:'||to_char(sysdate-03,'iw')||':ww:'||to_char(sysdate-03,'ww')||':w:'||to_char(sysdate-03,'w')||':D周:'||to_char(sysdate-03,'D') d8,
to_char(sysdate-02,'yyyymmdd')||':iw:'||to_char(sysdate-02,'iw')||':ww:'||to_char(sysdate-02,'ww')||':w:'||to_char(sysdate-02,'w')||':D周:'||to_char(sysdate-02,'D') d9,
to_char(sysdate-01,'yyyymmdd')||':iw:'||to_char(sysdate-01,'iw')||':ww:'||to_char(sysdate-01,'ww')||':w:'||to_char(sysdate-01,'w')||':D周:'||to_char(sysdate-01,'D') d1,
to_char(sysdate-00,'yyyymmdd')||':iw:'||to_char(sysdate-00,'iw')||':ww:'||to_char(sysdate-00,'ww')||':w:'||to_char(sysdate-00,'w')||':D周:'||to_char(sysdate-00,'D') d2,
to_char(sysdate+01,'yyyymmdd')||':iw:'||to_char(sysdate+01,'iw')||':ww:'||to_char(sysdate+01,'ww')||':w:'||to_char(sysdate+01,'w')||':D周:'||to_char(sysdate+01,'D') d3,
to_char(sysdate+02,'yyyymmdd')||':iw:'||to_char(sysdate+02,'iw')||':ww:'||to_char(sysdate+02,'ww')||':w:'||to_char(sysdate+02,'w')||':D周:'||to_char(sysdate+02,'D') d4,
to_char(sysdate+03,'yyyymmdd')||':iw:'||to_char(sysdate+03,'iw')||':ww:'||to_char(sysdate+03,'ww')||':w:'||to_char(sysdate+03,'w')||':D周:'||to_char(sysdate+03,'D') d5,
to_char(sysdate+04,'yyyymmdd')||':iw:'||to_char(sysdate+04,'iw')||':ww:'||to_char(sysdate+04,'ww')||':w:'||to_char(sysdate+04,'w')||':D周:'||to_char(sysdate+04,'D') d6,
to_char(sysdate+05,'yyyymmdd')||':iw:'||to_char(sysdate+05,'iw')||':ww:'||to_char(sysdate+05,'ww')||':w:'||to_char(sysdate+05,'w')||':D周:'||to_char(sysdate+05,'D') d7,
to_char(sysdate+06,'yyyymmdd')||':iw:'||to_char(sysdate+06,'iw')||':ww:'||to_char(sysdate+06,'ww')||':w:'||to_char(sysdate+06,'w')||':D周:'||to_char(sysdate+06,'D') d8,
to_char(sysdate+07,'yyyymmdd')||':iw:'||to_char(sysdate+07,'iw')||':ww:'||to_char(sysdate+07,'ww')||':w:'||to_char(sysdate+07,'w')||':D周:'||to_char(sysdate+07,'D') d9,
to_char(sysdate+08,'yyyymmdd')||':iw:'||to_char(sysdate+08,'iw')||':ww:'||to_char(sysdate+08,'ww')||':w:'||to_char(sysdate+08,'w')||':D周:'||to_char(sysdate+08,'D') d1,
to_char(sysdate+09,'yyyymmdd')||':iw:'||to_char(sysdate+09,'iw')||':ww:'||to_char(sysdate+09,'ww')||':w:'||to_char(sysdate+09,'w')||':D周:'||to_char(sysdate+09,'D') d2,
to_char(sysdate+10,'yyyymmdd')||':iw:'||to_char(sysdate+10,'iw')||':ww:'||to_char(sysdate+10,'ww')||':w:'||to_char(sysdate+10,'w')||':D周:'||to_char(sysdate+10,'D') d3,
to_char(sysdate+11,'yyyymmdd')||':iw:'||to_char(sysdate+11,'iw')||':ww:'||to_char(sysdate+11,'ww')||':w:'||to_char(sysdate+11,'w')||':D周:'||to_char(sysdate+11,'D') d4,
to_char(sysdate+12,'yyyymmdd')||':iw:'||to_char(sysdate+12,'iw')||':ww:'||to_char(sysdate+12,'ww')||':w:'||to_char(sysdate+12,'w')||':D周:'||to_char(sysdate+12,'D') d5,
to_char(sysdate+13,'yyyymmdd')||':iw:'||to_char(sysdate+13,'iw')||':ww:'||to_char(sysdate+13,'ww')||':w:'||to_char(sysdate+13,'w')||':D周:'||to_char(sysdate+13,'D') d6,
to_char(sysdate+14,'yyyymmdd')||':iw:'||to_char(sysdate+14,'iw')||':ww:'||to_char(sysdate+14,'ww')||':w:'||to_char(sysdate+14,'w')||':D周:'||to_char(sysdate+14,'D') d7,
to_char(sysdate+15,'yyyymmdd')||':iw:'||to_char(sysdate+15,'iw')||':ww:'||to_char(sysdate+15,'ww')||':w:'||to_char(sysdate+15,'w')||':D周:'||to_char(sysdate+15,'D') d8
from dual;
20200317:iw:12:ww:11:w:3:D周:3
20200318:iw:12:ww:12:w:3:D周:4
20200319:iw:12:ww:12:w:3:D周:5
20200320:iw:12:ww:12:w:3:D周:6
20200321:iw:12:ww:12:w:3:D周:7
20200322:iw:12:ww:12:w:4:D周:1
20200323:iw:13:ww:12:w:4:D周:2
20200324:iw:13:ww:12:w:4:D周:3
20200325:iw:13:ww:13:w:4:D周:4
20200326:iw:13:ww:13:w:4:D周:5
20200327:iw:13:ww:13:w:4:D周:6
20200328:iw:13:ww:13:w:4:D周:7
20200329:iw:13:ww:13:w:5:D周:1
20200330:iw:14:ww:13:w:5:D周:2
20200331:iw:14:ww:13:w:5:D周:3
20200401:iw:14:ww:14:w:1:D周:4
20200402:iw:14:ww:14:w:1:D周:5
20200403:iw:14:ww:14:w:1:D周:6
20200404:iw:14:ww:14:w:1:D周:7
20200405:iw:14:ww:14:w:1:D周:1
20200406:iw:15:ww:14:w:1:D周:2
20200407:iw:15:ww:14:w:1:D周:3
20200408:iw:15:ww:15:w:2:D周:4
20200409:iw:15:ww:15:w:2:D周:5
20200410:iw:15:ww:15:w:2:D周:6
20200411:iw:15:ww:15:w:2:D周:7
select to_char(TO_DATE('20110101', 'YYYYMMDD'), 'yyyyiw') as week, --oracle求当年的第几周 #常用,但是有一个问题,比如20181231是周一,但是却计算结果为201801,即2018第一周内。
to_char(TO_DATE('20110101', 'YYYYMMDD'), 'yyyyww') as week2, --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('20110401', 'YYYYMMDD'), 'yyyyW') as quarter, -- oracle求当年的第几季度
to_char(TO_DATE('20110401', 'YYYYMMDD'), 'yyyyw') as quarter, -- oracle求当年的第几季度
to_char(TO_DATE('20110401', 'YYYYMMDD'), 'yyyyq') as quarter -- oracle求当年的第几季度
from dual;
我自己实现的脚本:
select
T31267.CREATED_DATE as F31265,
(select to_char(to_date(T31267.CREATED_DATE,'yyyy-mm-dd'),'yyyyiw') from dual) as week
from LEAP.HMDM_DM_DLR_CLUE T31267
总结:
select to_char(TO_DATE('20110101', 'YYYYMMDD'), 'yyyyiw') as week, --oracle求当年的第几周 #常用,但是有一个问题,比如20181231是周一,但是却计算结果为201801,即2018第一周内。
to_char(TO_DATE('20110101', 'YYYYMMDD'), 'yyyyww') as week2, --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 -- oracle求当年的第几季度
from dual
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日的第一天作为每周第一天
这3个参数均为取每周第一天,区别如下:
ww,按每年的1月1日为第一周第一天,1月8日为第二周第一天……依此类推。
2010-1-1为2010年第一周第一天,取第一周的第一天,仍然为2010-1-1
SQL> select trunc(date'2010-1-1','w') from dual;
TRUNC(DATE'2010-1-1','W')
-------------------------
2010-1-1
2010-1-6为2010年第一周第六天,取第一周的第一天,为2010-1-1
SQL> select trunc(date'2010-1-6','w') from dual;
TRUNC(DATE'2010-1-6','W')
-------------------------
2010-1-1
2010-1-9为2010年第二周第二天,取这一周的第一天,为2010-1-8
SQL> select trunc(date'2010-1-9','w') from dual;
TRUNC(DATE'2010-1-9','W')
-------------------------
2010-1-8
w与ww功能类似,不过它是按每月的1日为每周第一天,例如1日为当月第一周第一天,8日为第二周第一天……依此类推。
那也就是说,1月份的参数w与ww用trunc函数的结果应该是一致的。
2010-3-1为当月第一周第一天,因此结果为2010-3-1
SQL> select trunc(date'2010-3-1','w') from dual;
TRUNC(DATE'2010-3-1','W')
-------------------------
2010-3-1
2010-3-8为当月第二周第一天,第二周第一天为2010-3-8
SQL> select trunc(date'2010-3-8','w') from dual;
TRUNC(DATE'2010-3-8','W')
-------------------------
2010-3-8
2010-3-9为当月第二周第二天,2010-3-8为第二周第一天
SQL> select trunc(date'2010-3-9','w') from dual;
TRUNC(DATE'2010-3-9','W')
-------------------------
2010-3-8
iw,不受月份与年份的影响,直接取日期参数的周一。
SQL> select trunc(date'2010-3-9','iw') from dual;
TRUNC(DATE'2010-3-9','IW')
--------------------------
2010-3-8
参考:
TRUNC(number,num_digits)
Number 需要截尾取整的数字。
Num_digits 用于指定取整精度的数字。Num_digits 的默认值为 0。如果Num_digits为正数,则截取小数点后Num_digits位;如果为负数,则先保留整数部分,然后从个位开始向前数,并将遇到的数字都变为0。
TRUNC()函数在截取时不进行四舍五入,直接截取。
针对数字的案例,如:
select trunc(123.458) from dual --123
select trunc(123.458,0) from dual --123
select trunc(123.458,1) from dual --123.4
select trunc(123.458,-1) from dual --120
select trunc(123.458,-4) from dual --0
select trunc(123.458,4) from dual --123.458
select trunc(123) from dual --123
select trunc(123,1) from dual --123
select trunc(123,-1) from dual --120
针对日期的案例,如:
select trunc(sysdate) from dual --2017/6/13 返回当天的日期
select trunc(sysdate,'yyyy') from dual --2017/1/1 返回当年第一天.
select trunc(sysdate,'mm') from dual --2017/6/1 返回当月第一天.
select trunc(sysdate,'d') from dual --2017/6/11 返回当前星期的第一天(以周日为第一天).
select trunc(sysdate,'dd') from dual --2017/6/13 返回当前年月日
select trunc(sysdate,'hh') from dual --2017/6/13 13:00:00 返回当前小时
select trunc(sysdate,'mi') from dual --2017/6/13 13:06:00 返回当前分钟
其他优秀链接推荐:
https://www.2cto.com/database/201302/191827.html
https://www.oschina.net/question/44870_3710
分类: oracle