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.

pchappAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

yesnaudCommented:
Hi,

here's what you can use

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


0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
yesnaudCommented:
sorry I forget something...

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

that's more like it...

cya
0
frankrCommented:
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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Oracle Database

From novice to tech pro — start learning today.