如何从周一开始分周

时间:2015-10-14 14:24:28

标签: sql-server sql-server-2008

我有一个问题,如何根据月份拆分周。实际上我的星期从星期一开始,在星期日从sql server周开始,这就产生了问题。我必须编写代码,因此如果用户给出的开始日期和结束日期的日期差异大于30且小于90,则日期应按天和周分开。这样做是这样的 - 首先,我必须找到开始日期是什么开始日期,如果开始日期是星期一,那么除了首先我必须找到我之前发现的上一个星期一时 -

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

但是如果开始日期是星期日,它就不会起作用我已经添加了一个变量来检查哪一天是开始日,如果它是星期天然后我将它设置为1然后我将我的代码更改为 - (对于仅限星期日)

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

对我而言,主要问题是如何分手。我按照给定的代码拆分了几周。 这是示例日期

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)

ouptut是 -

Week 0 November

它显示了11月1日至7日所有日期的第0周

任何人都可以帮助我,为什么它显示第0周而不是第2周,因为我的周从2015年1月和11月开始,从星期日开始,所以只有第1周,11月2日到11月8日应该是第2周 欢迎任何帮助请做评论,如果你不明白的东西我也尝试谷歌并首先尝试这个网站,但没有找到任何东西

编辑 - 想要一个逻辑,以便我能够从星期一到星期日分开一周,但我无法将其作为 -

如果月份在几周之间发生变化,那么一个月中的30个是星期二,那么本周应该只在这里结束,下个月1将在星期三现在新的星期从星期三到星期日开始,这将是第一周一个月。

在我的案例逻辑中,我已经得到了它,但具体到2015年11月,我无法得到它。

需要输出 -

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

2 个答案:

答案 0 :(得分:1)

问题在于你计算一周中某周的方式

这应该会给你想要的结果:

    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'; 

- 使用UDF

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

- 不使用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)

答案 1 :(得分:0)

这给出了正确的输出:

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

输出:

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