简体   繁体   English

获取当前周/月日期

[英]Get Current Week/Month Dates

I want to make function get dates of current week.我想让 function 获得本周的日期。 I will pass week start date number dynamically.Week start date can change.It can be sunday or monday or any day.I shoud be able to pass week start date number dynamically.我将动态传递周开始日期编号。周开始日期可以更改。它可以是星期日或星期一或任何一天。我应该能够动态传递周开始日期编号。

If i Call function GetCurrentWeekDates(weekStartDay NUMBER,callingday Date),i should get the result like the following如果我调用 function GetCurrentWeekDates(weekStartDay NUMBER,callingday Date),我应该得到如下结果

GetCurrentWeekDates(1,'10/04/2022') --1 is sunday GetCurrentWeekDates(1,'10/04/2022') --1 是星期日

Result should be as following结果应该如下

10/04/2022
11/04/2022
12/04/2022
13/04/2022
14/04/2022
15/04/2022
16/04/2022

and when GetCurrentWeekDates(1,11/04/2022) --1 is sunday当 GetCurrentWeekDates(1,11/04/2022) --1 是星期日

Result should be as following结果应该如下

11/04/2022
12/04/2022
13/04/2022
14/04/2022
15/04/2022
16/04/2022

and when GetCurrentWeekDates(1,'14/04/2022') --1 is sunday当 GetCurrentWeekDates(1,'14/04/2022') --1 是星期日

Result should be as following结果应该如下

14/04/2022
15/04/2022
16/04/2022

Similarly相似地

I want to make function to get dates of current month.我想制作 function 来获取当月的日期。 I will pass month number dynamically我将动态传递月份数

If i Call function GetCurrentMonthDates(monthNumber NUMBER,callingday Date),i should get the result like the following如果我调用 function GetCurrentMonthDates(monthNumber NUMBER,callingday Date),我应该得到如下结果

GetCurrentMonthDates(4,'01/04/2022') 4 is April GetCurrentMonthDates(4,'01/04/2022') 4 是四月

Result should be as following结果应该如下

01/04/2022
02/04/2022
.
.
30/04/2022

GetCurrentMonthDates(4,'16/04/2022') 4 is April GetCurrentMonthDates(4,'16/04/2022') 4 是四月

Result should be as following结果应该如下

16/04/2022
17/04/2022
.
.
30/04/2022

Here's a week option;这是一周的选择; I'll let you write the month function yourself (shouldn't be too difficult, now that you know how).我会让你自己写出月份function(应该不会太难,既然你知道怎么做)。

SQL> create or replace function f_week (par_start_day in varchar2, par_datum in date)
  2    return sys_refcursor
  3  is
  4    -- PAR_START_DAY: mon, tue, ..., sat, sun
  5    rc sys_refcursor;
  6  begin
  7    open rc for
  8      select par_datum + level - 1
  9      from dual
 10      connect by level <= next_Day(par_datum, par_start_day) - par_datum;
 11    return rc;
 12  end;
 13  /

Function created.

Testing:测试:

SQL> select f_week('sun', date '2022-04-10') from dual;

F_WEEK('SUN',DATE'20
--------------------
CURSOR STATEMENT : 1

CURSOR STATEMENT : 1

:B1+LEVEL-1
---------------
10.04.2022, sun
11.04.2022, mon
12.04.2022, tue
13.04.2022, wed
14.04.2022, thu
15.04.2022, fri
16.04.2022, sat

7 rows selected.

Some more testing:一些更多的测试:

SQL> select f_week('sun', date '2022-04-14') from dual;

F_WEEK('SUN',DATE'20
--------------------
CURSOR STATEMENT : 1

CURSOR STATEMENT : 1

:B1+LEVEL-1
---------------
14.04.2022, thu
15.04.2022, fri
16.04.2022, sat


SQL>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM