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.
| 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 theWeekWithinMonth2011-05-01 00:00:00.000 52011-05-02 00:00:00.000 12011-05-03 00:00:00.000 12011-05-04 00:00:00.000 12011-05-05 00:00:00.000 12011-05-06 00:00:00.000 12011-05-07 00:00:00.000 12011-05-08 00:00:00.000 22011-05-09 00:00:00.000 22011-05-10 00:00:00.000 22011-05-11 00:00:00.000 22011-05-12 00:00:00.000 22011-05-13 00:00:00.000 22011-05-14 00:00:00.000 22011-05-15 00:00:00.000 32011-05-16 00:00:00.000 32011-05-17 00:00:00.000 32011-05-18 00:00:00.000 32011-05-19 00:00:00.000 32011-05-20 00:00:00.000 32011-05-21 00:00:00.000 32011-05-22 00:00:00.000 42011-05-23 00:00:00.000 42011-05-24 00:00:00.000 42011-05-25 00:00:00.000 42011-05-26 00:00:00.000 42011-05-27 00:00:00.000 42011-05-28 00:00:00.000 42011-05-29 00:00:00.000 52011-05-30 00:00:00.000 52011-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 MVPhttp://visakhm.blogspot.com/ |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-07-05 : 14:36:39
|
try thisSELECT DATEPART(WK,INIT_ACTIVATION_DATE )-DATEPART(WK,DATEADD(mm,DATEDIFF(mm,0,INIT_ACTIVATION_DATE ),0))+1FROM table ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
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! |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-07-05 : 14:44:57
|
| welcome------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|