Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Extracting Week # from Date

Author  Topic 

funk.phenomena
Posting Yak Master

121 Posts

Posted - 2011-07-05 : 14:23:06
Hi There - I have a date column, for which I'm trying to extract the current "Calendar" week number (Sun-Sat) for the current month. So that it will only output a value between 1 to 5.

I'm using the current forumula:

DATEPART(DAY, INIT_ACTIVATION_DATE -1) / 7 + 1

It works great, except it always recognizes the first day of the month as the 5th week. Is there a way around this (aside from building a case) ?

Thanks!

INIT_ACTIVATION_DATE theWeekWithinMonth
2011-05-01 00:00:00.000 5
2011-05-02 00:00:00.000 1
2011-05-03 00:00:00.000 1
2011-05-04 00:00:00.000 1
2011-05-05 00:00:00.000 1
2011-05-06 00:00:00.000 1
2011-05-07 00:00:00.000 1
2011-05-08 00:00:00.000 2
2011-05-09 00:00:00.000 2
2011-05-10 00:00:00.000 2
2011-05-11 00:00:00.000 2
2011-05-12 00:00:00.000 2
2011-05-13 00:00:00.000 2
2011-05-14 00:00:00.000 2
2011-05-15 00:00:00.000 3
2011-05-16 00:00:00.000 3
2011-05-17 00:00:00.000 3
2011-05-18 00:00:00.000 3
2011-05-19 00:00:00.000 3
2011-05-20 00:00:00.000 3
2011-05-21 00:00:00.000 3
2011-05-22 00:00:00.000 4
2011-05-23 00:00:00.000 4
2011-05-24 00:00:00.000 4
2011-05-25 00:00:00.000 4
2011-05-26 00:00:00.000 4
2011-05-27 00:00:00.000 4
2011-05-28 00:00:00.000 4
2011-05-29 00:00:00.000 5
2011-05-30 00:00:00.000 5
2011-05-31 00:00:00.000 5

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-07-05 : 14:24:42
whats the purpose of that -1? so you want week which previous day belongs to?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-07-05 : 14:36:39
try this

SELECT DATEPART(WK,INIT_ACTIVATION_DATE )-DATEPART(WK,DATEADD(mm,DATEDIFF(mm,0,INIT_ACTIVATION_DATE ),0))+1
FROM table


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

funk.phenomena
Posting Yak Master

121 Posts

Posted - 2011-07-05 : 14:42:47
THANK YOU THANK YOU THANK YOU THANK YOU
Works like a charm!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-07-05 : 14:44:57
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -