Solved

Week# of Month

Posted on 1997-09-26
3
2,611 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.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Oracle Pivot 2 34
Oracle TEXT search question 9 29
query in Oracle forms Builder 2 27
SQL Retrieve Values 4 43
Introduction A previously published article on Experts Exchange ("Joins in Oracle", http://www.experts-exchange.com/Database/Oracle/A_8249-Joins-in-Oracle.html) makes a statement about "Oracle proprietary" joins and mixes the join syntax with gen…
Configuring and using Oracle Database Gateway for ODBC Introduction First, a brief summary of what a Database Gateway is.  A Gateway is a set of driver agents and configurations that allow an Oracle database to communicate with other platforms…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.

706 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now