3

我将计算每个星期一的上周日和上周六。

例如,今天是 2013 年 7 月 8 日星期一

上周日:2013 年 6 月 30 日 00:00:00

上周六:2013 年 7 月 6 日 23:59:59。

注意最后一个星期日是从 00:00:00 开始,最后一个星期六是直到 23:59:59

4

6 回答 6

13

对于那些希望在第一天是星期一的那一周获得最后一个周末(星期六和星期日)的人,这里有一个替代方案:

select today as todays_date,
       next_day(today - 7, 'sat') as prev_saturday, 
       next_day(today - 7, 'sun') as prev_sunday
from dual
于 2014-09-01T15:06:41.670 回答
8

查询获取最后一个星期天:

SELECT TRUNC(SYSDATE)-(select to_char(sysdate-1, 'd') FROM DUAL) FROM DUAL

查询获取最后一个星期六:

SELECT TRUNC(SYSDATE)-(select to_char(sysdate, 'd') from dual) FROM DUAL

如果您想获得之前的 SUNDAY/SATURDAY,则需要添加到 sysdate - 例如:

上周日:

SELECT TRUNC(SYSDATE)-(select to_char(sysdate, 'd')+6 FROM DUAL) FROM DUAL

上周六:

SELECT TRUNC(SYSDATE)-(select to_char(sysdate, 'd')+7 FROM DUAL) FROM DUAL

这种方法的优点是你可以在一周中的每一天享受这个查询

于 2016-08-31T13:33:12.063 回答
3

以下是从一周中的任何一天获取最后一个星期六或最后一个星期日的最快方法:

上个星期六:

select trunc(SYSDATE) - to_char(sysdate, 'd')  from dual;

上周日:

select trunc(SYSDATE) - (to_char(sysdate, 'd')-1)  from dual;

抱歉花了 6 年时间给你最好的答案。;)

于 2019-07-09T13:29:53.973 回答
2

鉴于您的问题,查询将仅在星期一运行并且目标是获取上述日期,解决它的一种方法是:

SELECT TRUNC(SYSDATE) AS TODAYS_DATE,
       TRUNC(SYSDATE)-8 AS PREVIOUS_SUNDAY,
       TRUNC(SYSDATE) - (INTERVAL '1' DAY + INTERVAL '1' SECOND) AS PREVIOUS_SATURDAY
  FROM DUAL

分享和享受。

于 2013-07-09T02:11:40.117 回答
0

这将更有用。您可以使用下面的查询从任何一天(不仅是星期一)取上周六或周日。

select NEXT_DAY(sysdate-7, 'SATURDAY') AS last_saturday,
       NEXT_DAY(sysdate-7, 'SUNDAY') AS last_sunday
from dual;
于 2022-02-15T03:41:23.830 回答
-1

使用以下 PLSQL 函数获取星期几。

使用函数获取月份中的日期。

SELECT
 DayofMonth(to_date('2020-03-29T01:00Z', 'yyyy-mm-dd"T"hh24:mi"Z"'), 'FRI', 'LAST') LASTFRI    -- 27-MAR-2020 01:00:00
 ,DayofMonth(to_date('2020-03-29T01:00Z', 'yyyy-mm-dd"T"hh24:mi"Z"'), 'SUN', 'FIRST') FIRSTSUN -- 01-MAR-2020 01:00:00
 ,DayofMonth(to_date('2020-03-29T01:00Z', 'yyyy-mm-dd"T"hh24:mi"Z"'), 'SUN', 'THIRD') THIRDSUN -- 15-MAR-2020 01:00:00
 
 ,DayofMonth(to_date('2020-02-02T01:01Z', 'yyyy-mm-dd"T"hh24:mi"Z"'), 'SUN', 'THIRD') THIRDSUN_FEB29  -- 16-FEB-2020 01:01:00
 ,DayofMonth(to_date('2021-02-02T01:01Z', 'yyyy-mm-dd"T"hh24:mi"Z"'), 'SUN', 'FOURTH') FouthSUN_FEB28 -- 28-FEB-2021 01:01:00
from dual;

PLSQL-Function:下面的函数标识月份的最后一天,使用TO_NUMBER()函数转换为数字作为参数传递给LAST_DAY()函数。

create or replace FUNCTION DayofMonth 
(dateOfMonth in date, dayOfWeek in varchar2 default 'SUN', weekOfMonth in varchar2 default 'LAST')
return date AS date_out date;

firstDayOfMonth number(5); lastDayOfMonth number(5);
monthFirstDayDate date; monthSecondDayDate date;
monthThirdDayDate date; monthFourthDayDate date;
monthLastDayDate date;
BEGIN

  SELECT TO_NUMBER(to_char( trunc(dateOfMonth) - (to_number(to_char(dateOfMonth,'DD')) - 1), 'DD'), '9G999D99') into firstDayOfMonth FROM dual;
  SELECT TO_NUMBER(to_char(trunc( add_months(trunc(dateOfMonth) - (to_number(to_char(dateOfMonth,'DD')) - 1), 1) -1 ), 'DD'), '9G999D99') into lastDayOfMonth FROM dual;
DBMS_OUTPUT.put_line('firstDayOfMonth:' || firstDayOfMonth || ', lastDayOfMonth:' || lastDayOfMonth);

select next_day(LAST_DAY(dateOfMonth) - (lastDayOfMonth - (7 * 0)), dayOfWeek) into monthFirstDayDate   FROM DUAL;
select next_day(LAST_DAY(dateOfMonth) - (lastDayOfMonth - (7 * 1)), dayOfWeek) into monthSecondDayDate  FROM DUAL;
select next_day(LAST_DAY(dateOfMonth) - (lastDayOfMonth - (7 * 2)), dayOfWeek) into monthThirdDayDate   FROM DUAL;
select next_day(LAST_DAY(dateOfMonth) - (lastDayOfMonth - (7 * 3)), dayOfWeek) into monthFourthDayDate  FROM DUAL;
select next_day(LAST_DAY(dateOfMonth) - (7),                        dayOfWeek) into monthLastDayDate    FROM DUAL;

DBMS_OUTPUT.put_line('monthFirstDayDate:'  || monthFirstDayDate || ', monthSecondDayDate:' || monthSecondDayDate);
DBMS_OUTPUT.put_line('monthThirdDayDate:'  || monthThirdDayDate || ', monthFourthDayDate:' || monthFourthDayDate);
DBMS_OUTPUT.put_line('monthLastDayDate:'   || monthLastDayDate);

IF (weekOfMonth = 'LAST') then
  date_out := monthLastDayDate;
ELSIF (weekOfMonth = 'FIRST') then
  date_out := monthFirstDayDate;
ELSIF (weekOfMonth = 'SECOND') then
  date_out := monthSecondDayDate;
ELSIF (weekOfMonth = 'THIRD') then
  date_out := monthThirdDayDate;
ELSIF (weekOfMonth = 'FOURTH') then
  date_out := monthFourthDayDate;
ELSE
  date_out := null;
END IF;

    return date_out;
EXCEPTION
    when others then
        DBMS_OUTPUT.put_line('Date:' || dateOfMonth || ', Day:' || dayOfWeek || ', Week:' || weekOfMonth || ' / ' || sqlcode || ' / ' || SQLERRM(sqlcode));
        date_out := null;
        return date_out;
END;
/

每月的第一天和最后一天 - Oracle SQL

SELECT
  to_char( trunc(sysdate) - (to_number(to_char(sysdate,'DD')) - 1), 'DD') as firstDay
  ,to_char(trunc( add_months(trunc(sysdate) - (to_number(to_char(sysdate,'DD')) - 1), 1) -1 ), 'DD') as lastDay
FROM dual;

在没有任何功能的情况下检查月份中的某天的测试:

select
    next_day(LAST_DAY(to_date('2021-01-29T01:01Z', 'yyyy-mm-dd"T"hh24:mi"Z"')) - 7, 'sun') as JAN_Month_Last_SUNDAY31 -- 31-JAN-2021 01:01:00
    ,next_day(LAST_DAY(to_date('2021-02-01T01:01Z', 'yyyy-mm-dd"T"hh24:mi"Z"')) - 7, 'sun') as FEB_Month_Last_SUNDAY28 -- 28-FEB-2021 01:01:00
    ,next_day(LAST_DAY(to_date('2021-04-01T01:01Z', 'yyyy-mm-dd"T"hh24:mi"Z"')) - 7, 'sun') as APRL_Month_Last_SUNDAY30 -- 25-APR-2021 01:01:00
    
    ,next_day(LAST_DAY(to_date('2021-01-29T01:01Z', 'yyyy-mm-dd"T"hh24:mi"Z"')) - 31, 'sun') as JAN_Month_First_SUNDAY31 -- 31-JAN-2021 01:01:00
    ,next_day(LAST_DAY(to_date('2021-02-01T01:01Z', 'yyyy-mm-dd"T"hh24:mi"Z"')) - 28, 'sun') as FEB_Month_First_SUNDAY28 -- 28-FEB-2021 01:01:00
    ,next_day(LAST_DAY(to_date('2021-04-01T01:01Z', 'yyyy-mm-dd"T"hh24:mi"Z"')) - 30, 'sun') as APRL_Month_First_SUNDAY30 -- 25-APR-2021 01:01:00
FROM DUAL;

select
  next_day(LAST_DAY(to_date('2021-01-29T01:01Z', 'yyyy-mm-dd"T"hh24:mi"Z"')) - 7, 'mon') as JAN_Month_Last_Mon31
  ,next_day(LAST_DAY(to_date('2021-01-29T01:01Z', 'yyyy-mm-dd"T"hh24:mi"Z"')) - 7, 'fri') as JAN_Month_Last_Fri31
  ,next_day(LAST_DAY(to_date('2021-01-29T01:01Z', 'yyyy-mm-dd"T"hh24:mi"Z"')) - (31 - (7 * 0)), 'fri') as JAN_Month_First_Fri31
  ,next_day(LAST_DAY(to_date('2021-01-29T01:01Z', 'yyyy-mm-dd"T"hh24:mi"Z"')) - (31 - (7 * 1)), 'fri') as JAN_Month_Second_Fri31
  ,next_day(LAST_DAY(to_date('2021-01-29T01:01Z', 'yyyy-mm-dd"T"hh24:mi"Z"')) - (31 - (7 * 2)), 'fri') as JAN_Month_Third_Fri31
  ,next_day(LAST_DAY(to_date('2021-01-29T01:01Z', 'yyyy-mm-dd"T"hh24:mi"Z"')) - (31 - (7 * 3)), 'fri') as JAN_Month_Fourth_Fri31
  ,next_day(LAST_DAY(to_date('2021-01-29T01:01Z', 'yyyy-mm-dd"T"hh24:mi"Z"')) - (31), 'fri') as JAN_Month_Last_Fri31
FROM DUAL;

select
  next_day(LAST_DAY(to_date('2021-02-02T01:01Z', 'yyyy-mm-dd"T"hh24:mi"Z"')) - (28 - (7 * 0)), 'fri') as FEB_Month_First_Fri28
  ,next_day(LAST_DAY(to_date('2021-02-02T01:01Z', 'yyyy-mm-dd"T"hh24:mi"Z"')) - (28 - (7 * 1)), 'fri') as FEB_Month_Second_Fri28
  ,next_day(LAST_DAY(to_date('2021-02-02T01:01Z', 'yyyy-mm-dd"T"hh24:mi"Z"')) - (28 - (7 * 2)), 'fri') as FEB_Month_Third_Fri28
  ,next_day(LAST_DAY(to_date('2021-02-02T01:01Z', 'yyyy-mm-dd"T"hh24:mi"Z"')) - (28 - (7 * 3)), 'SUN') as FEB_Month_Fourth_Fri28
  ,next_day(LAST_DAY(to_date('2021-02-02T01:01Z', 'yyyy-mm-dd"T"hh24:mi"Z"')) - (7), 'fri') as FEB_Month_Last_Fri28
FROM DUAL;

select
  next_day(LAST_DAY(to_date('2020-02-02T01:01Z', 'yyyy-mm-dd"T"hh24:mi"Z"')) - (29 - (7 * 0)), 'fri') as FEB_Month_First_Fri29
  ,next_day(LAST_DAY(to_date('2020-02-02T01:01Z', 'yyyy-mm-dd"T"hh24:mi"Z"')) - (29 - (7 * 1)), 'fri') as FEB_Month_Second_Fri29
  ,next_day(LAST_DAY(to_date('2020-02-02T01:01Z', 'yyyy-mm-dd"T"hh24:mi"Z"')) - (29 - (7 * 2)), 'fri') as FEB_Month_Third_Fri29
  ,next_day(LAST_DAY(to_date('2020-02-02T01:01Z', 'yyyy-mm-dd"T"hh24:mi"Z"')) - (29 - (7 * 3)), 'fri') as FEB_Month_Fourth_Fri29
  ,next_day(LAST_DAY(to_date('2020-02-02T01:01Z', 'yyyy-mm-dd"T"hh24:mi"Z"')) - (7), 'fri') as FEB_Month_Last_Fri29
FROM DUAL;
于 2020-12-22T14:34:01.937 回答