Where The Streets Have No Name

해당월의 시작일부터 말일까지 날짜출력 본문

Developement/DataBase

해당월의 시작일부터 말일까지 날짜출력

highheat 2006. 7. 18. 12:02
방법1)

SELECT x.date_val FROM (
select TRUNC(to_date('20060701'),'YYYY')+ROWNUM -1 as date_val,
         TO_CHAR(TRUNC(to_date('20060701'),'YYYY')+ROWNUM -1,'YYYYMM') as year_month,
         TO_CHAR(TRUNC(to_date('200607010'),'YYYY')+ROWNUM -1,'DD') as day,
       ROWNUM
FROM all_objects
WHERE TRUNC(to_date('20060701'),'YYYY')+ROWNUM -1 < ADD_MONTHS(TRUNC(to_date('20060701'),'YYYY'),12)
) x
where year_month = to_number(to_char(to_date('20060701'),'YYYYMM'));

방법2)

create or replace type tabDate as table of date;

create or replace function dates_in_month (p_month date)
return tabDate pipelined is
begin

for i in 0 .. to_number(add_months(p_month,1)-p_month-1) loop
    pipe row (p_month+i);
end loop;

return;
end;
/

select *
from table (dates_in_month(to_date('01-jan-2002','DD-mon-YYYY')));