我将在每个星期一的上周日和上周六计算。
E.g。今天是2013年7月8日星期一
上周日:2013年6月30日00:00:00
上周六:2013年7月6日23:59:59。
请注意,上周日为00:00:00,上周六为23:59:59
答案 0 :(得分:7)
对于那些希望在第一天是星期一的最后一个周末(星期六和星期日)的人来说,这里有另一种选择:
select today as todays_date,
next_day(today - 7, 'sat') as prev_saturday,
next_day(today - 7, 'sun') as prev_sunday
from dual
答案 1 :(得分:3)
查询获取最后一个星期日:
SELECT TRUNC(SYSDATE)-(select to_char(sysdate, 'd') FROM DUAL) FROM DUAL
查询获取最后一个星期六:
SELECT TRUNC(SYSDATE)-(select to_char(sysdate, 'd')+1 from dual) FROM DUAL
如果您想获得之前的SUNDAY / SATURDAY,则需要添加7 - 例如:
上周日:
SELECT TRUNC(SYSDATE)-(select to_char(sysdate, 'd')+7 FROM DUAL) FROM DUAL
上周六:
SELECT TRUNC(SYSDATE)-(select to_char(sysdate, 'd')+8 FROM DUAL) FROM DUAL
此方法的优点是您可以在一周中的每一天进行此查询享受
答案 2 :(得分: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
分享并享受。
答案 3 :(得分:0)
这是从一周中的任何日期获取最后一个星期六或最后一个星期日的最快方法:
上周六:
select trunc(SYSDATE) - to_char(sysdate, 'd') from dual;
上周日:
select trunc(SYSDATE) - (to_char(sysdate, 'd')-1) from dual;
道歉需要6年才能为您提供最佳答案。 ;)
答案 4 :(得分:-1)
使用以下 PLSQL 函数获取周的 DayDate。
使用函数获取月份的日期。
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;