create table DayTable(PKey primary key,yyyymm,sime) as
select 1,'200802',20 from dual union
select 2,'200802',27 from dual union
select 3,'200802',28 from dual union
select 4,'200802',29 from dual union
select 5,'200802',30 from dual union
select 6,'200802',31 from dual union
select 7,'200804',29 from dual union
select 8,'200804',30 from dual union
select 9,'200804',31 from dual union
select 10,'200805',29 from dual union
select 11,'200805',30 from dual union
select 12,'200805',31 from dual union
select 13,'200902',28 from dual union
select 14,'200902',29 from dual union
select 15,'200902',30 from dual;
col makeDay for a20
select PKey,yyyymm,sime,
case when matubi < sime
then to_char(to_date(yyyymm || '/' || to_char(matubi),'yyyymm/fmdd'),'yyyy-mm-dd')
else to_char(to_date(yyyymm || '/' || to_char(sime) ,'yyyymm/fmdd'),'yyyy-mm-dd')
end as makeDay
from (select PKey,yyyymm,sime,
extract(day from Last_day(to_date(yyyymm,'yyyymm'))) as matubi
from DayTable)
order by PKey;