Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Week Number within a month
Message
 
 
To
06/05/2003 12:42:21
Anderson Girardi
Athenas Automação de Escritório
Porto Alegre, Brazil
General information
Forum:
Microsoft SQL Server
Category:
SQL syntax
Miscellaneous
Thread ID:
00785333
Message ID:
00785545
Views:
14
Here is my function to determine the week number. I know my comments are not very good so feel free to ask me any questions.
* Purpose of this function to return week number based on Year/Month/Week
FUNCTION WeekOfDate
LPARAMETERS tnYear, tnMonth, tnWeek, cOption
*-- Option cOption has empty value if the week number is to 
*-- be determined by the first Monday of the month.
Local mon_1, date_1, day_1
Local i, firstdate, first_mon, nWeekNumber
Local cLastDayOfMonth, lIsLeapYear
Local nDayOfWeek
	
if pcount() < 4   
   cOption = " "
endif

*--- First of the year:
date_1 = CTOD(STR(tnmonth,2)+'/01/'+SUBSTR(STR(tnyear,4),3,2))
*--- Day of the week of the first date.
day_1  = DOW(date_1)

do case
   case !( cOption $ "FIL")		&& cOption has a number.

     *--- Day of the week
     nDayOfWeek = int(val( cOption ))

     *--- Make sure that nDayOfWeek is within valid range:
     if nDayOfWeek < 1 or nDayOfWeek > 7
        nDayOfWeek = 2
     endif

     *--- Scan through 7 days to find the first date matching Day of Week 
     *--- criterion.  For example, if passed nDayOfWeek is 4, find first
     *--- Wednesday of the month.
     for i = 1 to 7        
        if day_1 = nDayOfWeek
           exit
        endif  
        day_1  = iif( day_1 < 7, day_1 + 1, 1 )
        date_1 = date_1 + 1
     next

     *--- Increment this date by the Week Number.  That is, find 2nd ccurance
     *--- of Wednesday, Thursday, etc.
     date_1 = date_1 + (tnWeek - 1) * 7
			
   case cOption = "I"
     *--- First weekday of the month.
			
     if dow( date_1 ) = 1		&& Sunday
        date_1 = date_1 + 1  && Go to next Monday
     endif
     if dow( date_1 ) = 7    && Saturday
        date_1 = date_1 + 2  && Go to next Monday
     endif
			
     *--- Otherwise, use date "date_1" 
			
   case cOption = "L"
     *--- Last day of the month
		
     lIsLeapYear = (tnYear % 4 = 0) and ((tnYear % 100 # 0) OR (tnYear % 400 = 0))
			
     *--- Last day of the month as a character
     cLastDayOfMonth = substr( "31" + iif(lIsLeapYear,"29","28") + ;
					"31303130313130313031", (tnmonth*2-1), 2 )			
     *--- Last date of the month
     date_1 = CTOD(STR(tnmonth,2)+'/' + cLastDayOfMonth + '/'+SUBSTR(STR(tnyear,4),3,2))

     * NOTE: the DO CASE does not have a case for cOption = "F" since it is the same as the first day of the month "date_1"
		
endcase
								
*--- Find the closest Monday previous to the determined date.
*--- Reason: GPM needs Monday date to determine the week number.
for i = 1 to 7
   if dow( date_1 ) = 2      && Monday
      exit
   endif
   day_1  = iif( day_1 > 1, day_1 - 1, 7 )
   date_1 = date_1 - 1
next

firstdate = CTOD('01/01/'+SUBSTR(STR(YEAR(date_1),4),3,2))
IF DOW(firstdate) > 2
   first_mon = firstdate + 9 - DOW(firstdate)
ELSE
   first_mon = firstdate + 2 - DOW(firstdate)
ENDIF

nWeekNumber = int(( date_1 - first_mon) / 7 + 1)

RETURN (nWeekNumber)
>Hi Dmitry,
>
>Is exactly this what i want to do?
>
>Can you tell me more information about how do you do?
>
>Thanks
>
>Anderson Girardi
"The creative process is nothing but a series of crises." Isaac Bashevis Singer
"My experience is that as soon as people are old enough to know better, they don't know anything at all." Oscar Wilde
"If a nation values anything more than freedom, it will lose its freedom; and the irony of it is that if it is comfort or money that it values more, it will lose that too." W.Somerset Maugham
Previous
Reply
Map
View

Click here to load this message in the networking platform