Also consider NLS (session) settings
Michael, February 15, 2016 - 1:13 pm UTC
Some time ago I used exactly this method for a similiar task - and ran into strange issues because of different NLS settings...
To make this more robust we can specify the NLS language in the TO_CHAR call:
with dts as (
select date'2016-01-01'+rownum-1 dt from dual
connect by level <= 366
)
select * from dts
where to_char(dt, 'fmday', 'NLS_DATE_LANGUAGE=AMERICAN') = 'sunday';
February 15, 2016 - 5:56 pm UTC
Good advice. The date masks behave differently for some language settings, so it's good to be explicit.
most useful
A reader, August 23, 2016 - 1:35 pm UTC
great...!!! its works...thanks a lot
Using MOD to limit to counter
Josh Tyler, March 02, 2021 - 7:52 pm UTC
I use this other way to restrict the days without a WHERE condition
select to_char(dt, 'YYYY') || to_char(dt, 'WW') MailYearWk , pickupdt_Sun, scoredt_Wed, dt MailDate_Mon
, to_char(dt, 'DY') as Mail_Day
, to_char(dt, 'WW') as Mail_Week
from ( select distinct
next_day(trunc(sysdate,'YYYY') - 7 , 'SUN') + ((case when mod(level,7) = 0 then level else 0 end)) as pickupdt_Sun ,
next_day(trunc(sysdate,'YYYY') -7 , 'WED') + ((case when mod(level,7) = 0 then level else 0 end)) as scoredt_Wed ,
next_day(trunc(sysdate,'YYYY') , 'MON') + ((case when mod(level,7) = 0 then level else 0 end)) as dt
from dual connect by level <= ( select trunc(sysdate) + 14 - trunc(sysdate , 'YYYY') from dual) order by 1
)
March 03, 2021 - 8:52 am UTC
Good point, though I'd use something like:
( level - 1 ) * 7
instead of the case expression