我需要在SYSDATE
之前的最后一个星期五返回日期。
要明确:
答案 0 :(得分:7)
Oracle有一个名为next_day()
的便捷功能(参见here),它可以做一些相关的事情。它会在给定日期之后返回 next 中的某一天。你可以用它来得到你想要的东西:
select next_day(trunc(sysdate), 'FRIDAY') - 7
from dual;
注意:如果今天是星期五,那么这将返回今天的日期。我不确定这是不是你想要的。如果您想要上周五,请使用sysdate - 1
。
答案 1 :(得分:2)
为了完整起见,以下是NLS设置的独立方式:
WITH dates AS (SELECT TRUNC(SYSDATE, 'mm') + LEVEL -1 dt
FROM dual
CONNECT BY LEVEL <= 22)
SELECT dt,
to_char(dt, 'fmDay') day_of_dt,
TRUNC(dt + 3, 'iw') - 3 prev_fri_incl_today,
TRUNC(dt + 2, 'iw') - 3 prev_fri_not_incl_today
FROM dates;
DT DAY_OF_DT PREV_FRI_INCL_TODAY PREV_FRI_NOT_INCL_TODAY
----------- --------- ------------------- -----------------------
01/09/2016 Thursday 26/08/2016 26/08/2016
02/09/2016 Friday 02/09/2016 26/08/2016
03/09/2016 Saturday 02/09/2016 02/09/2016
04/09/2016 Sunday 02/09/2016 02/09/2016
05/09/2016 Monday 02/09/2016 02/09/2016
06/09/2016 Tuesday 02/09/2016 02/09/2016
07/09/2016 Wednesday 02/09/2016 02/09/2016
08/09/2016 Thursday 02/09/2016 02/09/2016
09/09/2016 Friday 09/09/2016 02/09/2016
10/09/2016 Saturday 09/09/2016 09/09/2016
11/09/2016 Sunday 09/09/2016 09/09/2016
12/09/2016 Monday 09/09/2016 09/09/2016
13/09/2016 Tuesday 09/09/2016 09/09/2016
14/09/2016 Wednesday 09/09/2016 09/09/2016
15/09/2016 Thursday 09/09/2016 09/09/2016
16/09/2016 Friday 16/09/2016 09/09/2016
17/09/2016 Saturday 16/09/2016 16/09/2016
18/09/2016 Sunday 16/09/2016 16/09/2016
19/09/2016 Monday 16/09/2016 16/09/2016
20/09/2016 Tuesday 16/09/2016 16/09/2016
21/09/2016 Wednesday 16/09/2016 16/09/2016
22/09/2016 Thursday 16/09/2016 16/09/2016
这依赖于以下事实:ISO周始终以星期一开始,并使用该偏差来推导上周五。
我已经提供了两个选项 - 如果你想今天返回,如果今天是星期五,则返回上一个星期五。
仅用于证明NLS设置的潜在问题(next_day
不允许您在调用时指定日期语言,与to_char
不同):
SQL> ALTER SESSION SET nls_date_language = 'french';
Session altered
SQL> select next_day(trunc(sysdate), 'FRIDAY') - 7
2 from dual;
select next_day(trunc(sysdate), 'FRIDAY') - 7
from dual
ORA-01846: not a valid day of the week
并证明我的解决方案仍然有效:
SQL> WITH dates AS (SELECT TRUNC(SYSDATE, 'mm') + LEVEL -1 dt
2 FROM dual
3 CONNECT BY LEVEL <= 22)
4 SELECT dt,
5 to_char(dt, 'fmDay') day_of_dt,
6 TRUNC(dt + 3, 'iw') - 3 prev_fri_incl_today,
7 TRUNC(dt + 2, 'iw') - 3 prev_fri_not_incl_today
8 FROM dates
9 ;
DT DAY_OF_DT PREV_FRI_INCL_TODAY PREV_FRI_NOT_INCL_TODAY
----------- --------- ------------------- -----------------------
01/09/2016 Jeudi 26/08/2016 26/08/2016
02/09/2016 Vendredi 02/09/2016 26/08/2016
03/09/2016 Samedi 02/09/2016 02/09/2016
04/09/2016 Dimanche 02/09/2016 02/09/2016
05/09/2016 Lundi 02/09/2016 02/09/2016
06/09/2016 Mardi 02/09/2016 02/09/2016
07/09/2016 Mercredi 02/09/2016 02/09/2016
08/09/2016 Jeudi 02/09/2016 02/09/2016
09/09/2016 Vendredi 09/09/2016 02/09/2016
10/09/2016 Samedi 09/09/2016 09/09/2016
11/09/2016 Dimanche 09/09/2016 09/09/2016
12/09/2016 Lundi 09/09/2016 09/09/2016
13/09/2016 Mardi 09/09/2016 09/09/2016
14/09/2016 Mercredi 09/09/2016 09/09/2016
15/09/2016 Jeudi 09/09/2016 09/09/2016
16/09/2016 Vendredi 16/09/2016 09/09/2016
17/09/2016 Samedi 16/09/2016 16/09/2016
18/09/2016 Dimanche 16/09/2016 16/09/2016
19/09/2016 Lundi 16/09/2016 16/09/2016
20/09/2016 Mardi 16/09/2016 16/09/2016
21/09/2016 Mercredi 16/09/2016 16/09/2016
22/09/2016 Jeudi 16/09/2016 16/09/2016
22 rows selected