oracle 在oracle中查找星期几
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12815968/
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
Finding day of the week in oracle
提问by venkatKA
to_char(sysdate,'Day')
returns the current day of the week. What I want is to get the date
of the most recent "sunday" that passed. Of course we can go for a complex query to get it done. But is there any simple way I'm not aware of?
to_char(sysdate,'Day')
返回一周中的当前日期。我想要的是获得date
最近过去的“星期日”。当然,我们可以通过一个复杂的查询来完成它。但是有没有我不知道的简单方法?
回答by Rohit Chaudhari
You can do it with
你可以用
SELECT NEXT_DAY(SYSDATE-8, 'SUN') FROM DUAL;
here
这里
SYSDATE-8
returns the day before 8 days &
8 天前返回 &
NEXT_DAY(mydate, 'SUN')
returns the next sunday to it
下个星期天返回给它
回答by Jo?o Barreto
You can do it with a query as simple as this:
您可以使用像这样简单的查询来做到这一点:
SELECT dt
FROM ( SELECT SYSDATE - LEVEL - 1 dt
FROM DUAL
CONNECT BY LEVEL < 8)
WHERE TO_CHAR ( dt, 'd' ) = 1
回答by venkatKA
I think NEXT_DAY(SYSDATE-6, 'SUN')
would work. Assuming current day as sunday, I go back 6 days (i.e. last monday) and so when I look for the next sunday, I'd get the sysdate
itself. Whereas, next_day(sysdate-8,'SUN')
could be used to get the last day of previous week. Thanks for your efforts.
我认为NEXT_DAY(SYSDATE-6, 'SUN')
会奏效。假设今天是星期天,我返回 6 天(即上一个星期一),所以当我寻找下一个星期天时,我会得到它sysdate
本身。而,next_day(sysdate-8,'SUN')
可用于获取前一周的最后一天。感谢您的努力。
回答by user3670021
SELECT dt
FROM ( SELECT to_date('5/23/2014') - LEVEL + 1 dt
FROM DUAL
CONNECT BY LEVEL < 8)
WHERE TO_CHAR ( dt, 'd' ) = 1
I changed LEVEL - 1 dt
to LEVEL + 1 dt
to get it to work to find the previous Sunday.
我更改LEVEL - 1 dt
为LEVEL + 1 dt
让它工作以找到上一个星期天。