0

i have problem to calculate the week where the data just display four week. How calculate if there are five week?. i don't know how to do. Anybody know about this?.:(

OMG Ponies
  • 325,700
  • 82
  • 523
  • 502
Amin SCO
  • 1,892
  • 7
  • 31
  • 49
  • A bit more context wold be helpful – Andrew Cooper Jul 27 '12 at 04:29
  • 3
    @AndrewCooper: Not context, but effort... – OMG Ponies Jul 27 '12 at 04:31
  • Your question is very unclear, but similar ones have been asked [numerous times](http://stackoverflow.com/search?q=sql+week+number) before on this site. One common answer is not to calculate anything, but to use a [calendar table](http://stackoverflow.com/questions/2459260/calendar-table-week-number-of-month). – Pondlife Jul 27 '12 at 12:45

3 Answers3

0

select count(datepart(dw,datecoln)) as col1 from YourTable group by datepart(dw,datecoln) having month(datecoln)=@inputMonth

if you get and any record with the value 5 in col1..

0

Ideally we will not have 5 weeks in a month.

AnandPhadke
  • 13,160
  • 5
  • 26
  • 33
-1

Here the logic.

Calculate week number of given date

find first day of the month and also find its week number (say start)

find last day of month and also find its week number (say end)

number off week in current date = end-start

DECLARE @GivenDate datetime
DECLARE @start BIGINT
DECLARE @end BIGINT
SET @givendate=getdate()--You can supply your date

SET @start =  (SELECT DATEPART(wk,(SELECT DATEADD(mm,DATEDIFF(mm,0,@GivenDate),0))))
SET @end =  (SELECT DATEPART(wk,(SELECT DATEADD(ms,- 3,DATEADD(mm,0,DATEADD(mm,DATEDIFF(mm,0,@GivenDate)+1,0))))))

SELECT (@end-@start)+1 AS NoOfWeeks

Refer this to find week number

Or Refer this post

Community
  • 1
  • 1
Pintu Paul
  • 389
  • 3
  • 18