Look:
I made a table that contains dates from 1 to the other month:
Select all
select trunc(to_date('01-mar-2008'),'mm')+rownum-1 dt
from all_objects
where rownum<=35
ie:
Select all
SQL> select trunc(to_date('01-mar-2008'),'mm')+rownum-1 dt
2 from all_objects
3 where rownum<=35
4 /
DT
---------
01-MAR-08
02-MAR-08
03-MAR-08
04-MAR-08
05-MAR-08
06-MAR-08
07-MAR-08
08-MAR-08
09-MAR-08
10-MAR-08
11-MAR-08
12-MAR-08
13-MAR-08
14-MAR-08
15-MAR-08
16-MAR-08
17-MAR-08
18-MAR-08
.
.
.
That we have this table, I did like this:
Select all
SELECT
dt
, TO_CHAR( dt, 'DY' , 'NLS_DATE_LANGUAGE=PORTUGUESE' ) dia
, CASE
WHEN dt - 8+TO_CHAR( TRUNC(dt,'MM'), 'D') < TRUNC(dt,'MM') THEN 1
ELSE TO_CHAR (dt - 8+TO_CHAR( TRUNC(dt,'MM'), 'D') , 'W')+1
END semana
FROM (SELECT TRUNC(TO_DATE('01-MAR-2008'),'MM')+ROWNUM-1 dt
FROM all_objects
WHERE ROWNUM<=35
)
Let us go to the tests:
Select all
SQL> SELECT
2 dt
3 , TO_CHAR( dt, 'DY' , 'NLS_DATE_LANGUAGE=PORTUGUESE' ) dia
4 , CASE
5 WHEN dt - 8+TO_CHAR( TRUNC(dt,'MM'), 'D') < TRUNC(dt,'MM') THEN 1
6 ELSE TO_CHAR (dt - 8+TO_CHAR( TRUNC(dt,'MM'), 'D') , 'W')+1
7 END semana
8 FROM (SELECT TRUNC(TO_DATE('01-MAR-2008'),'MM')+ROWNUM-1 dt
9 FROM all_objects
10 WHERE ROWNUM<=35
11 )
12 /
DT DIA SEMANA
--------- --------- ----------
01-MAR-08 SAB 1
02-MAR-08 DOM 2
03-MAR-08 SEG 2
04-MAR-08 TER 2
05-MAR-08 QUA 2
06-MAR-08 QUI 2
07-MAR-08 SEX 2
08-MAR-08 SAB 2
09-MAR-08 DOM 3
10-MAR-08 SEG 3
11-MAR-08 TER 3
DT DIA SEMANA
--------- --------- ----------
12-MAR-08 QUA 3
13-MAR-08 QUI 3
14-MAR-08 SEX 3
15-MAR-08 SAB 3
16-MAR-08 DOM 4
17-MAR-08 SEG 4
18-MAR-08 TER 4
19-MAR-08 QUA 4
20-MAR-08 QUI 4
21-MAR-08 SEX 4
22-MAR-08 SAB 4
DT DIA SEMANA
--------- --------- ----------
23-MAR-08 DOM 5
24-MAR-08 SEG 5
25-MAR-08 TER 5
26-MAR-08 QUA 5
27-MAR-08 QUI 5
28-MAR-08 SEX 5
29-MAR-08 SAB 5
30-MAR-08 DOM 6
31-MAR-08 SEG 6
01-APR-08 TER 1
02-APR-08 QUA 1
DT DIA SEMANA
--------- --------- ----------
03-APR-08 QUI 1
04-APR-08 SEX 1
35 rows selected.
SQL>
Let's go to another month (June ):
Select all
SQL> SELECT
2 dt
3 , TO_CHAR( dt, 'DY' , 'NLS_DATE_LANGUAGE=PORTUGUESE' ) dia
4 , CASE
5 WHEN dt - 8+TO_CHAR( TRUNC(dt,'MM'), 'D') < TRUNC(dt,'MM') THEN 1
6 ELSE TO_CHAR (dt - 8+TO_CHAR( TRUNC(dt,'MM'), 'D') , 'W')+1
7 END semana
8 FROM (SELECT TRUNC(TO_DATE('01-jun-2008'),'MM')+ROWNUM-1 dt
9 FROM all_objects
10 WHERE ROWNUM<=35
11 )
12 /
DT DIA SEMANA
--------- --------- ----------
01-JUN-08 DOM 1
02-JUN-08 SEG 1
03-JUN-08 TER 1
04-JUN-08 QUA 1
05-JUN-08 QUI 1
06-JUN-08 SEX 1
07-JUN-08 SAB 1
08-JUN-08 DOM 2
09-JUN-08 SEG 2
10-JUN-08 TER 2
11-JUN-08 QUA 2
DT DIA SEMANA
--------- --------- ----------
12-JUN-08 QUI 2
13-JUN-08 SEX 2
14-JUN-08 SAB 2
15-JUN-08 DOM 3
16-JUN-08 SEG 3
17-JUN-08 TER 3
18-JUN-08 QUA 3
19-JUN-08 QUI 3
20-JUN-08 SEX 3
21-JUN-08 SAB 3
22-JUN-08 DOM 4
DT DIA SEMANA
--------- --------- ----------
23-JUN-08 SEG 4
24-JUN-08 TER 4
25-JUN-08 QUA 4
26-JUN-08 QUI 4
27-JUN-08 SEX 4
28-JUN-08 SAB 4
29-JUN-08 DOM 5
30-JUN-08 SEG 5
01-JUL-08 TER 1
02-JUL-08 QUA 1
03-JUL-08 QUI 1
DT DIA SEMANA
--------- --------- ----------
04-JUL-08 SEX 1
05-JUL-08 SAB 1
35 rows selected.
SQL>
It must probably exist an easier way !!!
This topic always helps me in this hour:
http://en.glufke.net/oracle/viewtopic.php?t=22
: -O