In Oracle, the first day of a week is determined by NLS_TERRITORY parameter. So use a value of NLS_TERRITORY where Sunday is the first weekday, for example, AMERICA .
In Oracle, the day of the week depends on the NLS_TERRITORY session setting:
SELECT TO_CHAR(DATE '2022-12-11', 'D') AS day, -- a sunday
(SELECT value FROM NLS_SESSION_PARAMETERS WHERE parameter = 'NLS_TERRITORY')
AS territory
FROM DUAL;
Outputs different values for different parameters. I.e., if you use:
ALTER SESSION SET NLS_TERRITORY='America';
ALTER SESSION SET NLS_TERRITORY='Germany';
ALTER SESSION SET NLS_TERRITORY='India';
ALTER SESSION SET NLS_TERRITORY='Oman';
ALTER SESSION SET NLS_TERRITORY='Bangladesh';
and run the query for each then the outputs are: | DAY | TERRITORY | | ------------ | ------------ | | 1 | AMERICA | | 7 | GERMANY | | 1 | INDIA | | 2 | OMAN | | 3 | BANGLADESH | So you could just pick the correct territory for your database, i.e.:
ALTER SESSION SET NLS_TERRITORY='India';
Option 2: Compare to the start of the ISO-week
Or if you want to be independent of the session settings then you can compare it to the start of the ISO-week (which is always midnight Monday) and offset by one day to change the comparison from Monday-Sunday to Sunday-Saturday:
SELECT TRUNC(SYSDATE + 1) - TRUNC(SYSDATE + 1, 'IW') + 1 AS day
FROM DUAL;
Which, for today (Friday) outputs: | DAY | | ------------ | | 6 | fiddle
4条答案
按热度按时间cx6n0qe31#
Talk to your DBA to fix territory. If it was set to e.g. India, then you wouldn't have to calculate anything:
rta7y2nd2#
In Oracle, the first day of a week is determined by
NLS_TERRITORY
parameter. So use a value ofNLS_TERRITORY
where Sunday is the first weekday, for example,AMERICA
.fiddle
dwthyt8l3#
Option 1:
NLS_TERRITORY
session parameterIn Oracle, the day of the week depends on the
NLS_TERRITORY
session setting:Outputs different values for different parameters. I.e., if you use:
and run the query for each then the outputs are:
| DAY | TERRITORY |
| ------------ | ------------ |
| 1 | AMERICA |
| 7 | GERMANY |
| 1 | INDIA |
| 2 | OMAN |
| 3 | BANGLADESH |
So you could just pick the correct territory for your database, i.e.:
Option 2: Compare to the start of the ISO-week
Or if you want to be independent of the session settings then you can compare it to the start of the ISO-week (which is always midnight Monday) and offset by one day to change the comparison from Monday-Sunday to Sunday-Saturday:
Which, for today (Friday) outputs:
| DAY |
| ------------ |
| 6 |
fiddle
lawou6xi4#
我找到了这个问题的答案,非常简单