1

I have a problem how to split week according to month. Actually my week start from Monday and in sql server week start from Sunday and that creates the problem. I have to write the code so date should be split up in days and weeks if the start date and end date given by user has date difference more than 30 and less than 90. This is done like this- First I have to find that what is the start date if the the start date falls on Monday then its okay other than that first I have to find when is the previous monday for this I found-

declare @enddate date = '2015-08-01';               
select convert (date, DATEADD(wk, DATEDIFF(wk, 0,@enddate),0))

But it won't work if the start date is Sunday for this I have added a variable which checks what day is start day if it is Sunday then I set it to 1 then I changed my code to- (for Sunday only)

declare @enddate date = '2015-08-02';               
select convert (date, DATEADD(wk, DATEDIFF(wk, 6,@enddate),0))

The main problem for me is in weeks how to the split up. I am spliting up weeks by the given code. This is example date

declare @enddate date = '2015-11-03';      
SELECT 'Week ' + CAST(DATEPART(WEEK,@ENDDATE) 
- DATEPART(WEEK,DATENAME(YEAR,@ENDDATE) + '/' 
+ DATENAME(MONTH,@ENDDATE) + '/' + '1') AS NVARCHAR)+ ' ' + DATENAME(MONTH,@ENDDATE)

The ouptut is-

Week 0 November

And it is showing week 0 for all dates between 1 and 7 November

Can anyone help me why it is showing week 0 not week 2 as my week starts from Monday and November 2015 starts from Sunday so that only day should be week 1 and from 2 Nov to 8 Nov should be week 2 Any help is welcomed please do comment if you dont understand something I also tried to google and tried this website itself first but dont find anything

EDITED- want a logic so that I will able to split week from Monday to Sunday but I am not able to get it as-

If month is changed in between weeks such that 30 of one month is Tuesday so this week should end here only and next month 1 would be on Wednesday now new week for month starts from Wednesday to Sunday that will be week one for that month.

In my case logic I have got it but for specifically November 2015 I am not able to get it.

output required-

Start date -2015-09-07        End date-2015-11-01

Output 
Start date                                      End date
Week 2 September                            Week 1 November



Start date -2015-09-07              End date-2015-11-05

Output -

Start date                                      End date
Week 2 September                             Week 1 November
2015-11-02                                   2015-11-05

Start date -2015-09-07              End date-2015-11-08

Output -

Start date                                      End date
Week 2 September                             Week 2 November

Start date -2015-09-08              End date-2015-11-05

Start date                                      End date
2015-09-08                                   2015-09-13
Week 3 September                             Week 1 November
2015-11-02                                   2015-11-05
6
  • As posted this doesn't make a lot of sense. There is a lot of text but it does not paint a clear picture of what you are trying to do.
    – Sean Lange
    Oct 14, 2015 at 14:37
  • 1
    I don't understand when it start/ I see no start date. Can you add clear example with start and end date and their respective output. Your current example does not make a lot of sense right now. Oct 14, 2015 at 14:46
  • Can you check it again Oct 14, 2015 at 14:54
  • You want your calculations to be masen on the fact that mobday is the first day of week? Oct 14, 2015 at 14:56
  • @JulienVavasseur for 3 Nov it should show week 2 for which date you run also it is showing week 1 for because I added 1 before n varchar and because of that my whole code changed...so I dont want it also Oct 14, 2015 at 14:57

2 Answers 2

1

The problem is the way you are calculation the week of month

This should give you the desired result:

    SET DATEFIRST 1;
go
    alter function WeekOfMonth (@Day Date) returns int
    as 
    begin  
      return  DATEPART(wk, @Day) - DATEPART(wk, CAST(DATEADD(month, DATEDIFF(month, 0, @day), 0) AS DATE)) + 1
    end
    go
    declare @startdate date = '2015-09-07'; 
    declare @enddate date = '2015-11-03'; 

-With use of a UDF

SELECT  @startdate as "Start date",  'Week ' + cast(dbo.WeekOfMonth(@startdate) as NVARCHAR) + ' ' + DATENAME(MONTH,@ENDDATE)

-Without use of a UDF

SELECT  @enddate as "End date",  'Week ' + cast(DATEPART(wk, @enddate) - DATEPART(wk, CAST(DATEADD(month, DATEDIFF(month, 0, @enddate), 0) AS DATE)) + 1 as NVARCHAR) + ' ' + DATENAME(MONTH,@ENDDATE)
6
  • 1
    But for both the dates it is showing wrong week...I want week 2 for date = '2015-9-7' and also week 2 for @enddate = '2015-11-05'; Oct 14, 2015 at 15:03
  • Nops man actually problem is with month November you can check also for date 2015-11-01 it is showing week 4 october also 2015-11-02 for this it is showing week 1 that should be week 2 Oct 14, 2015 at 15:11
  • 1
    sorry but in my database concat is recognised as built in fucntion can you post it in another way also my input is only date not datetime Oct 14, 2015 at 15:19
  • can you just give me that in simple select I dont know how will use as I have many case scenarios and I can't use that function all time..sorry but please can you do it Oct 14, 2015 at 15:29
  • Let us continue this discussion in chat. Oct 14, 2015 at 15:31
0

This give the correct output:

declare @data table(id int identity(0, 1), startdate date, enddate date)
insert into @data(startdate, enddate) values
    ('20150906', '20151101')
    , ('20150907', '20151102')
    , ('20150907', '20151103')
    , ('20150907', '20151104')
    , ('20150907', '20151105')
    , ('20150907', '20151108')
    , ('20150908', '20151108')
    , ('20150908', '20151109')


SET DATEFIRST 1

-- days before weeks
Select id, type = 0, startdate, enddate
    , [Start Date] = Cast(startdate as varchar(10)), [End Date] = Cast(DATEADD(DAY, (7-DATEPART(WEEKDAY, startdate))%7, startdate) as varchar(10))
From @data
Where DATEPART(WEEKDAY, startdate) <> 1
Union All
-- Weeks
Select id, type = 1, startdate, enddate
    , 'Week ' + Cast((
        DATEPART(WEEK, startdate) - DATEPART(WEEK, DATEADD(MONTH, DATEDIFF(MONTH, 0, startdate), 0)) + 1 + Case When DATEPART(WEEKDAY, startdate) <> 1 then 1 else 0 end
    ) as varchar(10)) + ' ' + DATENAME(MONTH,startdate)
    , 'Week ' + Cast(DATEPART(WEEK, enddate) - DATEPART(WEEK, DATEADD(MONTH, DATEDIFF(MONTH, 0, enddate), 0)) + 1 as varchar(10)) + ' ' + DATENAME(MONTH,enddate)
From @data
Union All
-- Days after weeks
Select id, type = 2, startdate, enddate
    , cast(DATEADD(DAY, -(DATEPART(WEEKDAY, enddate)+6)%7, enddate) as varchar(10))
    , cast(enddate as varchar(10))
From @data
Where DATEPART(WEEKDAY, enddate) <> 7
Order By id, type

Output:

id  type    startdate   enddate     Start Date          End Date
0   0       2015-09-06  2015-11-01  2015-09-06          2015-09-06
0   1       2015-09-06  2015-11-01  Week 2 September    Week 1 November
1   1       2015-09-07  2015-11-02  Week 2 September    Week 2 November
1   2       2015-09-07  2015-11-02  2015-11-02          2015-11-02
2   1       2015-09-07  2015-11-03  Week 2 September    Week 2 November
2   2       2015-09-07  2015-11-03  2015-11-02          2015-11-03
3   1       2015-09-07  2015-11-04  Week 2 September    Week 2 November
3   2       2015-09-07  2015-11-04  2015-11-02          2015-11-04
4   1       2015-09-07  2015-11-05  Week 2 September    Week 2 November
4   2       2015-09-07  2015-11-05  2015-11-02          2015-11-05
5   1       2015-09-07  2015-11-08  Week 2 September    Week 2 November
6   0       2015-09-08  2015-11-08  2015-09-08          2015-09-13
6   1       2015-09-08  2015-11-08  Week 3 September    Week 2 November
7   0       2015-09-08  2015-11-09  2015-09-08          2015-09-13
7   1       2015-09-08  2015-11-09  Week 3 September    Week 3 November
7   2       2015-09-08  2015-11-09  2015-11-09          2015-11-09
6
  • actually in my sample it is week 3 because the week is not full so we will go to next Sunday as day then we can get the full week as next day is Monday and from there we can count whole week Oct 14, 2015 at 15:21
  • sorry I changed the sample you can look it Oct 14, 2015 at 15:31
  • yeah both should have same week as week 2 November and 20151101 should be week 1 and 20151109 should be week 3 Oct 14, 2015 at 15:36
  • ok. I will update my answer by you should update your sample. it is incorrect for 20151105. Oct 14, 2015 at 15:37
  • actually I want like if start date is monday and end date is Sunday then only week should be shown other than that remaining days should be shown so in my sample you can see that Oct 14, 2015 at 15:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.