Solved

Week# of Month

Posted on 1997-09-26
3
2,615 Views
Last Modified: 2009-08-05
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.

0
Comment
Question by:pchapp
  • 2
3 Comments
 
LVL 1

Accepted Solution

by:
yesnaud earned 200 total points
ID: 1080636
Hi,

here's what you can use

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


0
 
LVL 1

Expert Comment

by:yesnaud
ID: 1080637
sorry I forget something...

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

that's more like it...

cya
0
 
LVL 2

Expert Comment

by:frankr
ID: 1080638
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

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Note: this article covers simple compression. Oracle introduced in version 11g release 2 a new feature called Advanced Compression which is not covered here. General principle of Oracle compression Oracle compression is a way of reducing the d…
How to Create User-Defined Aggregates in Oracle Before we begin creating these things, what are user-defined aggregates?  They are a feature introduced in Oracle 9i that allows a developer to create his or her own functions like "SUM", "AVG", and…
This video explains at a high level with the mandatory Oracle Memory processes are as well as touching on some of the more common optional ones.
This video shows how to copy a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question