我需要生成两个给定日期之间的天数列表。 查询应该类似于
Select ... from ... where "date" is between date1 and date2
' 2014-05-12'和' 2014-05-16'应该是
Monday | 2014-05-12
Tuesday | 2014-05-13
Wednesday| 2014-05-14
Thursday | 2014-05-15
Friday | 2014-05-16
开始和结束日期永远不会超过1个月,将来也是如此。 我最好不要制作所有日期的表格或视图,但如果那是唯一的解决方案......
我找到了generate days from date range,但是给定的查询给出了错误,我根本不理解它,所以无法开始找到可能导致错误的内容。
感谢您的帮助
答案 0 :(得分:2)
with days as (
select date '2014-05-12' + (level - 1) as day
from dual
connect by level <= date '2014-05-16' - date '2014-05-12' + 1
)
select *
from days
order by day;
SQLFiddle:http://sqlfiddle.com/#!4/d41d8/29558
如果您需要定期使用此功能,最好为您创建一个功能:
create or replace type date_list as table of date
/
create or replace function generate_series(p_start_date date, p_end_date date)
return date_list
pipelined
is
l_count integer;
begin
l_count := trunc(p_end_date) - trunc(p_start_date);
for i in 0..l_count loop
pipe row (p_start_date + i);
end loop;
end;
/