Solved

Week# of Month

Posted on 1997-09-26
3
2,621 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Truncate is a DDL Command where as Delete is a DML Command. Both will delete data from table, but what is the difference between these below statements truncate table <table_name> ?? delete from <table_name> ?? The first command cannot be …
When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

739 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