Link to home
Start Free TrialLog in
Avatar of pchapp
pchapp

asked on

Week# of Month

Hi

We writing some queries for reporting. We needs
to know the week # of the month the day falls on (eg. Sept. 21, 1997
should fall on week #4).

We used something like:

     select to_char(to_date('21-09-1997','DD-MM-YYYY'),'W')
     from   dual;

However, this is not working as we had expected it would.  For example,
in September 1997, week #1 should be from the 1-6, #2 from 7-13, #3 from
14-20, #4 from 21-27, and #5 from 28-30.  However, the "W" date
conversion seems to consider a week to start, on the very day the
month starts (eg. for September, the 1st falls on a Monday, therefore
the week spans returned by the "W" conversion incorrectly reports the
week #'s as (in the case of September), #1: 1-7, #2: 8-14, #3: 15-21,
#4: 22-28, #5: 29-30.

Any help would be greatly appreciated.

Thanks in advance.


Prabha.

ASKER CERTIFIED SOLUTION
Avatar of yesnaud
yesnaud

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of yesnaud
yesnaud

sorry I forget something...

week := trunc ((to_number (to_char (date,'dd'))/7))+1;

that's more like it...

cya
I think the proposed answer will work for the date in question, but that it isn't a general solution.  Try the following.  It's coded as a SQL*Plus script that you can paste into a file and execute.  It will prompt you for a date each time you run it, so you can try dates from different months to check for correct behavior.  I'll discuss the approach to the problem after the code.

/*  CALENDAR_WEEK.SQL

    This script will prompt for a date in Oracle standard format
    (DD-Mon-YY), and return a number corresponding to the week
    that date occurs in the month.

    It assumes that each week begins on a Sunday (hence the
    name).
   
*/

SELECT TO_NUMBER (TO_CHAR (TO_DATE ('&&DATE_IN'), 'W')) +
       DECODE (SIGN (TO_NUMBER
                       (TO_CHAR
                          (LAST_DAY
                             (ADD_MONTHS ('&&DATE_IN', -1)) + 1,
                             'D'
                          )
                       ) - TO_NUMBER
                              (TO_CHAR
                                 (TO_DATE ('&&DATE_IN'), 'D')
                              )
                    ),
               1,
                  1,
                  0) the_week
  FROM DUAL
/
UNDEFINE date_in

Discussion of algorithm:

The Oracle date function to return the week assumes that weeks are all 7 days in length, beginning with the first day of the month.  If the month begins on Thursday, each week runs from Thursday through Wednesday.

The request asks for each week to begin on Sunday (so the first week may have from 1 to 7 days), which I'll call a 'calendar week'.

I used the Oracle date conversion as a base.  It either agrees with the calendar week or understates it by one.  The algorithm decides which is true for a given date, and either adds 0 or 1 as required.

The first question is 'When is the Oracle week wrong'?  If the first day of the month is Wednesday, the Oracle week is incorrect  for dates that fall on Sunday through Tuesday (and we need to add 1 to the result of the Oracle date function to correct it).  It's correct if the date in question is Wed - Sat.

The solution is to compare the day of the week of the day in question to the day of the week of the first day of the month, and add 1 to the Oracle date if the former is less than the latter.

The script converts each date to a numeric day of the week (i.e. 1 through 7), subtracts the date in question from the first day of the month, and examines the sign of the result.  If the result is positive, add 1 to the Oracle week.  If 0 or negative, add 0.