From 52546801261581ddc2e373a8417886444ae802f0 Mon Sep 17 00:00:00 2001 From: Morten Braten Date: Thu, 24 Sep 2015 19:08:17 +0200 Subject: [PATCH] Added get_date_tab to generate table of dates --- demos/date_util_pkg_demo.sql | 6 +++++ doc/changelog.txt | 7 ++++++ ora/date_util_pkg.pkb | 46 ++++++++++++++++++++++++++++++++++++ ora/date_util_pkg.pks | 5 ++++ 4 files changed, 64 insertions(+) diff --git a/demos/date_util_pkg_demo.sql b/demos/date_util_pkg_demo.sql index 799b9e7..49a44fe 100755 --- a/demos/date_util_pkg_demo.sql +++ b/demos/date_util_pkg_demo.sql @@ -3,3 +3,9 @@ select * from table(date_util_pkg.explode_month(2011, 2)) +-- generate a table of dates based on a "calendar string" +-- for calendar string syntax, see http://docs.oracle.com/cd/B28359_01/appdev.111/b28419/d_sched.htm#i1009923 +-- a start and stop date can be specified, it not specified it defaults to a year from now + +select t.column_value +from table(date_util_pkg.get_date_tab('FREQ=WEEKLY; BYDAY=MON,WED,FRI', trunc(sysdate), trunc(sysdate+90))) t \ No newline at end of file diff --git a/doc/changelog.txt b/doc/changelog.txt index 83685d8..87da56e 100755 --- a/doc/changelog.txt +++ b/doc/changelog.txt @@ -1,4 +1,11 @@ +Version 1.7.1 +============= + +- Added sms_util_pkg +- Minor enhancements to date_util_pkg +- Minor enhancements to string_util_pkg + Version 1.7.0 ============= - Split installation script into various modules (core, xml, amazon, microsoft, etc.) diff --git a/ora/date_util_pkg.pkb b/ora/date_util_pkg.pkb index 2ca8b69..b08eeb1 100755 --- a/ora/date_util_pkg.pkb +++ b/ora/date_util_pkg.pkb @@ -628,5 +628,51 @@ begin end explode_month; +function get_date_tab (p_calendar_string in varchar2, + p_from_date in date := null, + p_to_date in date := null) return t_date_array pipelined +as + l_from_date date := coalesce(p_from_date, sysdate); + l_to_date date := coalesce(p_to_date, add_months(l_from_date,12)); + l_date_after date; + l_next_date date; +begin + + /* + + Purpose: get table of dates based on specified calendar string + + Remarks: see https://oraclesponge.wordpress.com/2010/08/18/generating-lists-of-dates-in-oracle-the-dbms_scheduler-way/ + see http://www.kibeha.dk/2014/12/date-row-generator-with-dbmsscheduler.html + + Who Date Description + ------ ---------- -------------------------------- + MBR 24.09.2015 Created + + */ + + l_date_after := l_from_date - 1; + + loop + + dbms_scheduler.evaluate_calendar_string ( + calendar_string => p_calendar_string, + start_date => l_from_date, + return_date_after => l_date_after, + next_run_date => l_next_date + ); + + exit when l_next_date > l_to_date; + + pipe row (l_next_date); + + l_date_after := l_next_date; + + end loop; + + return; + +end get_date_tab; + end date_util_pkg; / diff --git a/ora/date_util_pkg.pks b/ora/date_util_pkg.pks index 6883f65..b9c2e89 100755 --- a/ora/date_util_pkg.pks +++ b/ora/date_util_pkg.pks @@ -103,5 +103,10 @@ as function explode_month (p_year in number, p_month in number) return t_period_date_tab pipelined; + -- get table of dates based on specified calendar string + function get_date_tab (p_calendar_string in varchar2, + p_from_date in date := null, + p_to_date in date := null) return t_date_array pipelined; + end date_util_pkg; /