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>
From:http://tw.wingwit.com/Article/program/Oracle/201311/18269.html