确定 Oracle 日期是否在周末?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3450965/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Determine if Oracle date is on a weekend?
提问by Marcus Leon
Is this the best way to determine if an Oracle date is on a weekend?
这是确定 Oracle 日期是否在周末的最佳方法吗?
select * from mytable
where
TO_CHAR (my_date, 'DY', 'NLS_DATE_LANGUAGE=ENGLISH') IN ('SAT', 'SUN');
回答by ninesided
As of Oracle 11g, yes. The only viable region agnostic alternative that I've seen is as follows:
从 Oracle 11g 开始,是的。我见过的唯一可行的区域不可知替代方案如下:
SELECT *
FROM mytable
WHERE MOD(TO_CHAR(my_date, 'J'), 7) + 1 IN (6, 7);
回答by Guru
Not an answer to the question. But some more information. There are many more SQL tricks with date.
不是问题的答案。但更多的信息。还有更多关于日期的 SQL 技巧。
to_char(sysdate, 'd') --- day of a week, 1,2,3 .. to 7
to_char(sysdate, 'dd') --- day of a month, 1,2,3 .. to 30 or 31
to_char(sysdate, 'ddd') --- day of a year, 1,2,3 .. to 365 or 366
to_char(sysdate, 'w') --- week of a month, 1,2,3,4 or 5
to_char(sysdate, 'ww') --- week of a year, 1,2,3 .. to 52
More here: to_char
function.
更多信息:to_char
函数。
回答by Jan-Hendrik van Heusden
MOD(TO_CHAR(my_date, 'J'), 7) + 1 IN (6, 7)
is NOT correct! The day number of weekend days may be 6 & 7, or 7 and 1, depending on the NLS-settings.
是不正确的!周末的天数可能是 6 和 7,或 7 和 1,具体取决于 NLS 设置。
So the PROPER test (irrespective of NLS-settings) is this one mentioned before:
所以正确的测试(不考虑 NLS 设置)是前面提到的:
TO_CHAR (my_date, 'DY', 'NLS_DATE_LANGUAGE=ENGLISH') IN ('SAT', 'SUN')
回答by Victor
In my opinion the best option is
在我看来最好的选择是
TO_CHAR (my_date, 'DY', 'NLS_DATE_LANGUAGE=ENGLISH') IN ('SAT', 'SUN')
TO_CHAR (my_date, 'DY', 'NLS_DATE_LANGUAGE=ENGLISH') IN ('SAT', 'SUN')
回答by Joakim
set NLS_TERRITORY before
之前设置 NLS_TERRITORY
alter session set NLS_TERRITORY=SWEDEN;
So you are sure which numbers are weekends
所以你确定哪些数字是周末