oracle周数计算
===========================================================
作者: keyneslin()
发表于: :
分类: ORACLE
出处:
================================================
日期计算算第n周的第一天及最后一天是几号 by keynes
================================================
ww的算法为每年月日为第一周开始date+为每一周结尾
例如为第一周的第一天而第一周的最后一天为+=
公式 每周第一天 date + 周 *
每周最后一天date + 周 *
不管怎么编排格式都会跑掉真气人 ~><~
=========================================================================
日期计算算第n周的第一天及最后一天是几号 by keynes
=========================================================================
ww的算法为每年月日为第一周开始date+为每一周结尾
例如为第一周的第一天而第一周的最后一天为+=
公式 每周第一天 date + 周 *
每周最后一天date + 周 *
如果以ww格式为主第周的起迄如下
:asdb:WF>select to_date(yyyymmdd) + *to_date(yyyymmdd) + * from dual;
TO_DATE( TO_DATE(
JAN JAN
:asdb:WF>select to_date(yyyymmdd) + *to_date(yyyymmdd) + * from dual;
TO_DATE( TO_DATE(
APR APR
Elapsed: ::
验证如下
:asdb:WF>select to_char(to_date(yyyymmdd)ww) as weeknto_char(to_date(yyyymmdd)ww) as weekto_char(to_date(yyyymmdd)ww) as weekto_char(to_date(yyyymmdd)ww) as weekn from dual;
WEEK WEEK WEEK WEEK
Elapsed: ::
:asdb:WF>
iw的算法为星期一至星期日算一周且每年的第一个星期一为第一周
例如为星期六所以用iw的算法是前年的周而之后才是第一周的开始
公式 每周第一天 next_day(date) + 周 *
每周最后一天next_day(date) + 周 *
如果以iw格式为主第周的起迄如下
:asdb:WF>select next_day(to_date(yyyymmdd)MONDAY)+ * as first_daynext_day(to_date(yyyymmdd)MONDAY)+ * as last_day from dual;
FIRST_DAY LAST_DAY
JAN JAN
Elapsed: ::
:asdb:WF>
:asdb:WF>select next_day(to_date(yyyymmdd)MONDAY)+ * as first_daynext_day(to_date(yyyymmdd)MONDAY)+ * as last_day from dual;
FIRST_DAY LAST_DAY
APR MAY
Elapsed: ::
:asdb:WF>
验证如下
:asdb:WF>select to_char(to_date(yyyymmdd)iw) as weeknto_char(to_date(yyyymmdd)iw) as weekto_char(to_date(yyyymmdd)iw) as weekto_char(to_date(yyyymmdd)iw) as weekn from dual;
WEEK WEEK WEEK WEEK
Elapsed: ::
其它
== 查今天是 本月 的第几周
SELECT TO_CHAR(SYSDATEWW) TO_CHAR(TRUNC(SYSDATEMM)WW) + AS weekOfMon from dual;
或
SELECT TO_CHAR(SYSDATEW) AS weekOfMon from dual;
== 查今天是 今年 的第几周
select to_char(sysdateww) from dual;
或
select to_char(sysdateiw) from dual;
附注
上文所提之iw及ww格式在doc内解释如下
IW = Week of year ( or ) based on the ISO standard
WW = Week of year () where week starts on the first day of the year and continues to the seventh day of the year
参考文件
Format Models
oracle周数计算(续)
===========================================================
作者: keyneslin()
发表于: :
分类: ORACLE
出处:
本篇是接续前一篇因有朋友(allenc)要计算oracle的周数日期起迄
所以测试了一下
测试结果如下
特殊周数计算
星期日到星期六为一周(与ww及iw算法不同)
例年的第一周起迄是同一天是
例年的第一周起为迄为
计算第一周的天数
create or replace function fdf(p_date in date) return number
is
begin
检查是否传入要计算那一年的一月一日
if to_char(to_char(p_dateddd)) <> then
return null;
end if;
如果第一周的第一天刚好也是最后一天时传回
if to_char(p_dated) <> then
return (next_day(p_dateSATURDAY) p_date +);
else
return ;
end if;
exception
when others then
dbms_outputput_line(sqlerrm);
end;
计算公式
起 decode(周计算当年的一月一日计算当年的一月一日 + (fdf(计算当年的一月一日) + (周 )*)) as 起
迄(第一种算法)decode(周to_date(to_char(计算当年的一月一日yyyy)||||yyyymmdd)计算当年的一月一日 + (fdf(计算当年的一月一日) + (周)*)) as 迄
迄(第二种算法)decode(周last_day(trunc(计算当年的一月一日mm)+)计算当年的一月一日 + (fdf(计算当年的一月一日) + (周)*)) as 迄
上面的参数部份计算当年的一月一日为date type周为number type
传回值一律是date型态
例如
年第周
select decode(to_date(yyyymmdd)to_date(yyyymmdd) + (fdf(to_date(yyyymmdd)) + ( )*)) as s_weekdecode(to_date(to_char(to_date(yyyymmdd)yyyy)||||yyyymmdd)to_date(yyyymmdd) + (fdf(to_date(yyyymmdd)) + ()*)) as e_week from dual;
年第周
select decode(to_date(yyyymmdd)to_date(yyyymmdd) + (fdf(to_date(yyyymmdd)) + ( )*)) as s_weekdecode(to_date(to_char(to_date(yyyymmdd)yyyy)||||yyyymmdd)to_date(yyyymmdd) + (fdf(to_date(yyyymmdd)) + ()*)) as e_week from dual;
年第周
select decode(to_date(yyyymmdd)to_date(yyyymmdd) + (fdf(to_date(yyyymmdd)) + ( )*)) as s_weekdecode(to_date(to_char(to_date(yyyymmdd)yyyy)||||yyyymmdd)to_date(yyyymmdd) + (fdf(to_date(yyyymmdd)) + ()*)) as e_week from dual;
年第周
select decode(to_date(yyyymmdd)to_date(yyyymmdd) + (fdf(to_date(yyyymmdd)) + ( )*)) as s_weekdecode(to_date(to_char(to_date(yyyymmdd)yyyy)||||yyyymmdd)to_date(yyyymmdd) + (fdf(to_date(yyyymmdd)) + ()*)) as e_week from dual;
年第周
select decode(to_date(yyyymmdd)to_date(yyyymmdd) + (fdf(to_date(yyyymmdd)) + ( )*)) as s_weekdecode(to_date(to_char(to_date(yyyymmdd)yyyy)||||yyyymmdd)to_date(yyyymmdd) + (fdf(to_date(yyyymmdd)) + ()*)) as e_week from dual;
年第周
select decode(to_date(yyyymmdd)to_date(yyyymmdd) + (fdf(to_date(yyyymmdd)) + ( )*)) as s_weekdecode(to_date(to_char(to_date(yyyymmdd)yyyy)||||yyyymmdd)to_date(yyyymmdd) + (fdf(to_date(yyyymmdd)) + ()*)) as e_week from dual;
年第周方法
select decode(to_date(yyyymmdd)to_date(yyyymmdd) + (fdf(to_date(yyyymmdd)) + ( )*)) as s_weekdecode(last_day(trunc(to_date(yyyymmdd)mm)+)to_date(yyyymmdd) + (fdf(to_date(yyyymmdd)) + ()*)) as e_week from dual;
结果如下
:asdb:WF>年第周
:asdb:WF>select decode(to_date(yyyymmdd)to_date(yyyymmdd) + (fdf(to_date(yyyymmdd)) + ( )*)) as s_weekdecode(to_date(to_char(to_date(yyyymmdd)yyyy)||||yyyymmdd)to_date(yyyymmdd) + (fdf(to_date(yyyymmdd)) + ()*)) as e_week from dual;
S_WEEK E_WEEK
JAN JAN
Elapsed: ::
:asdb:WF>年第周
:asdb:WF>select decode(to_date(yyyymmdd)to_date(yyyymmdd) + (fdf(to_date(yyyymmdd)) + ( )*)) as s_weekdecode(to_date(to_char(to_date(yyyymmdd)yyyy)||||yyyymmdd)to_date(yyyymmdd) + (fdf(to_date(yyyymmdd)) + ()*)) as e_week from dual;
S_WEEK E_WEEK
JAN JAN
Elapsed: ::
:asdb:WF>年第周
:asdb:WF>select decode(to_date(yyyymmdd)to_date(yyyymmdd) + (fdf(to_date(yyyymmdd)) + ( )*)) as s_weekdecode(to_date(to_char(to_date(yyyymmdd)yyyy)||||yyyymmdd)to_date(yyyymmdd) + (fdf(to_date(yyyymmdd)) + ()*)) as e_week from dual;
S_WEEK E_WEEK
DEC DEC
Elapsed: ::
:asdb:WF>
:asdb:WF>年第周
:asdb:WF>select decode(to_date(yyyymmdd)to_date(yyyymmdd) + (fdf(to_date(yyyymmdd)) + ( )*)) as s_weekdecode(to_date(to_char(to_date(yyyymmdd)yyyy)||||yyyymmdd)to_date(yyyymmdd) + (fdf(to_date(yyyymmdd)) + ()*)) as e_week from dual;
S_WEEK E_WEEK
JAN JAN
Elapsed: ::
:asdb:WF>年第周
:asdb:WF>select decode(to_date(yyyymmdd)to_date(yyyymmdd) + (fdf(to_date(yyyymmdd)) + ( )*)) as s_weekdecode(to_date(to_char(to_date(yyyymmdd)yyyy)||||yyyymmdd)to_date(yyyymmdd) + (fdf(to_date(yyyymmdd)) + ()*)) as e_week from dual;
S_WEEK E_WEEK
JAN JAN
Elapsed: ::
:asdb:WF>年第周
:asdb:WF>select decode(to_date(yyyymmdd)to_date(yyyymmdd) + (fdf(to_date(yyyymmdd)) + ( )*)) as s_weekdecode(to_date(to_char(to_date(yyyymmdd)yyyy)||||yyyymmdd)to_date(yyyymmdd) + (fdf(to_date(yyyymmdd)) + ()*)) as e_week from dual;
S_WEEK E_WEEK
DEC DEC
Elapsed: ::
:asdb:WF>