|
|
|
|
|
|
| Re: Find Out first thursday for each month [message #577288 is a reply to message #577279] |
Thu, 14 February 2013 01:07   |
 |
Michel Cadot
Messages: 54127 Registered: March 2007 Location: Nanterre, France, http://...
|
Senior Member Account Moderator |
|
|
sss111ind wrote on Thu, 14 February 2013 06:58
SELECT next_day(to_date('01-'
||LEVEL
||'-2013','dd-mm-yyyy'),'thursday') first_thursday
FROM dual
CONNECT BY LEVEL<=12;
Wrong! See august:
SQL> SELECT next_day(to_date('01-'
2 ||LEVEL
3 ||'-2013','dd-mm-yyyy'),'thursday') first_thursday
4 FROM dual
5 CONNECT BY LEVEL<=12;
FIRST_THURS
-----------
03-JAN-2013
07-FEB-2013
07-MAR-2013
04-APR-2013
02-MAY-2013
06-JUN-2013
04-JUL-2013
08-AUG-2013
05-SEP-2013
03-OCT-2013
07-NOV-2013
05-DEC-2013
It should be 1st August.
One correct version is:
SQL> select next_day(add_months(trunc(sysdate,'YEAR')-1,level-1),'THU')
2 from dual
3 connect by level <= 12
4 /
NEXT_DAY(AD
-----------
03-JAN-2013
07-FEB-2013
07-MAR-2013
04-APR-2013
02-MAY-2013
06-JUN-2013
04-JUL-2013
01-AUG-2013
05-SEP-2013
03-OCT-2013
07-NOV-2013
05-DEC-2013
Regards
Michel
|
|
|
|
|
|
|
|
|
|
| Re: Find Out first thursday for each month [message #577365 is a reply to message #577339] |
Thu, 14 February 2013 08:30  |
Solomon Yakobson
Messages: 1397 Registered: January 2010
|
Senior Member |
|
|
NLS-independent solution:
with test as (
select add_months(trunc(sysdate,'YEAR'),level-1) datum
from dual
connect by level <= 12
)
select trunc(datum + 3,'iw') + 3 -- 3 is # of days between Thursday and Monday
from test
order by 1
/
TRUNC(DATUM
-----------
03-JAN-2013
07-FEB-2013
07-MAR-2013
04-APR-2013
02-MAY-2013
06-JUN-2013
04-JUL-2013
01-AUG-2013
05-SEP-2013
03-OCT-2013
07-NOV-2013
TRUNC(DATUM
-----------
05-DEC-2013
12 rows selected.
SQL>
SY.
[Updated on: Thu, 14 February 2013 08:30] Report message to a moderator
|
|
|
|