如何在 Oracle 查询中选择两个给定日期之间的日期?
如何在 Oracle 查询中选择两个给定日期之间的日期?
How do I select dates between two given dates in an Oracle query?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
如何在 Oracle 查询中选择两个给定日期之间的日期?
How do I select dates between two given dates in an Oracle query?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(7)
来自
http://forums。 devshed.com/oracle-development-96/select-all-dates- Between-two-dates-92997.html
from
http://forums.devshed.com/oracle-development-96/select-all-dates-between-two-dates-92997.html
您可以以一种棘手的方式使用 LEVEL 伪列来生成一系列,因此,例如,要获取今天到 20 天后的天数列表,我可以:
一般来说,您可以看到这如何适用于任何两个给定日期。
如果您还想包含两个结束日期,您可以调整条款。
You can use the LEVEL pseudocolumn in a tricky way to generate a series, so, for example, to get the list of days between today and 20 days from now I can:
Generically you can see how this would apply for any two given dates.
And you can adjust the clauses if you want to include the two end dates as well.
您还可以使用下面的方法获取日期范围内的日历日期列表(类似于 Michael Broughton 的解决方案)
You could also use the below to get a list of calendar dates between a date range (similar to Michael Broughton's solution)
我经常为我开发的调度应用程序执行此操作,因此我创建了一个管道表函数。有时我需要几天、几小时或 15 分钟的时间间隔。这不完全是我的功能,因为我的代码位于包中。例如,在这里,我获取 2020 年 1 月 1 日到 2020 年 1 月 10 日之间的天数:
管道函数:
I do this so often for a scheduling app I work on that I created a pipelined table function. Sometimes I need days, hours or 15 minutes between times. This is not exactly my function, because my code is in a package. For example, here, I'm getting days between Jan 1 2020 and Jan 10 2020:
The pipelined function:
使用“之间”。一般意义上:
请注意,dateCol 被定义为日期,date1 和 date2 也是日期值。如果这些不是日期,那么您将使用 to_date 函数将它们转换为日期。
Use "between". In a general sense:
note that dateCol is defined as a date and date1 and date2 are also date values. If these aren't dates, then you'll convert them to dates using to_date function.
with all_days as (select trunc(to_date('12-03-2017','dd-mm-yyyy')+levl)-1 as all_dates from
(从双连接中选择级别 level < (sysdate-to_date('12-03-2017','DD-MM-YYYY')+1) )
排序依据 1)
select count(*) as no_of_days from all_days where ltrim(rtrim(to_char(all_dates,'DAY'))) not in ('SATURDAY','SUNDAY');
with all_days as (select trunc(to_date('12-03-2017','dd-mm-yyyy')+levl)-1 as all_dates from
(select level levl from dual connect by level < (sysdate-to_date('12-03-2017','DD-MM-YYYY')+1) )
order by 1)
select count(*) as no_of_days from all_days where ltrim(rtrim(to_char(all_dates,'DAY'))) not in ('SATURDAY','SUNDAY');