2

我需要从每月的指定(1/2/3/4/5)周找到开始日期和结束日期。这样我就可以获得 Date_Created 在指定(1/2/3/4/5)周内出现的记录。

例如,如果

I choose 1st week of febuary 2013 then 
I want startdate = 2/1/2013 and enddate = 2/2/2013
I choose 2nd week of febuary 2013 then 
I want startdate = 2/3/2013 and enddate = 2/9/2013

休息周也是如此。

我在数据库端有 SQL Server 2008。如果有人有想法,请分享。

4

4 回答 4

3

这是一个查询,用于获取给定月份的所有周开始和结束日期。所以你可以从这个查询周中选择你需要的:

SQLFiddle 示例

with C(i) as 
( select CAST('2013-02-01' as datetime) i
  UNION ALL
  select DATEADD (day,1,i) i from C 
    where DATEADD (day,1,i)
          <DATEADD(month,1,'2013-02-01')
), C1 as
(
select DATEPART(WEEK,i)-DATEPART(WEEK,'2013-02-01')+1 WeekOfMonth,i from C
)
select WeekOfMonth,min(i) StartDate, max(i) EndDate from C1 group by WeekOfMonth
于 2013-02-20T11:57:07.963 回答
1

我写了一个程序来解决我的问题,它将以周号(1/2/3/4/5)作为输入,并将返回开始日期和结束日期

ALTER PROCEDURE dbo.SPGetStartAndEndDateofSpcifiedWeek
    @Week int
AS
    SET NOCOUNT ON 
    DECLARE @date DateTime
    DECLARE @currdate DateTime
    DECLARE @startdate DateTime
    DECLARE @enddate DateTime
    DECLARE @CurrWeek int

    /*SET @date  = CONVERT(date,GETDATE())*/
    SET @currdate  = CONVERT(date,GETDATE())
    SET @CurrWeek = datediff(week, dateadd(week, datediff(week, 0, dateadd(month, datediff(month, 0, GETDATE()), 0)), 0), GETDATE() - 1) + 1

    IF (@Week = 1)
    BEGIN
        IF (@CurrWeek = 1)
        BEGIN SET @date = @currdate END
        IF (@CurrWeek = 2)
        BEGIN SET @date = @currdate - 7 END
        IF (@CurrWeek = 3)
        BEGIN SET @date = @currdate - 14 END
        IF (@CurrWeek = 4)
        BEGIN SET @date = @currdate - 21 END
        IF (@CurrWeek = 5)
        BEGIN SET @date = @currdate - 28 END
    END
    IF (@Week = 2)
    BEGIN
        IF (@CurrWeek = 1)
        BEGIN SET @date = @currdate + 7 END
        IF (@CurrWeek = 2)
        BEGIN SET @date = @currdate  END
        IF (@CurrWeek = 3)
        BEGIN SET @date = @currdate - 7 END
        IF (@CurrWeek = 4)
        BEGIN SET @date = @currdate - 14 END
        IF (@CurrWeek = 5)
        BEGIN SET @date = @currdate - 21 END
    END
    IF (@Week = 3)
    BEGIN
        IF (@CurrWeek = 1)
        BEGIN SET @date = @currdate + 14 END
        IF (@CurrWeek = 2)
        BEGIN SET @date = @currdate + 7 END
        IF (@CurrWeek = 3)
        BEGIN SET @date = @currdate END
        IF (@CurrWeek = 4)
        BEGIN SET @date = @currdate - 7 END
        IF (@CurrWeek = 5)
        BEGIN SET @date = @currdate - 14 END
    END
    IF (@Week = 4)
    BEGIN
        IF (@CurrWeek = 1)
        BEGIN SET @date = @currdate + 21 END
        IF (@CurrWeek = 2)
        BEGIN SET @date = @currdate + 14 END
        IF (@CurrWeek = 3)
        BEGIN SET @date = @currdate + 7 END
        IF (@CurrWeek = 4)
        BEGIN SET @date = @currdate END
        IF (@CurrWeek = 5)
        BEGIN SET @date = @currdate - 7 END
    END
    IF (@Week = 5)
    BEGIN
        IF (@CurrWeek = 1)
        BEGIN SET @date = @currdate + 28 END
        IF (@CurrWeek = 2)
        BEGIN SET @date = @currdate + 21 END
        IF (@CurrWeek = 3)
        BEGIN SET @date = @currdate + 14 END
        IF (@CurrWeek = 4)
        BEGIN SET @date = @currdate + 7 END
        IF (@CurrWeek = 5)
        BEGIN SET @date = @currdate END
    END

    Select  CONVERT(date, DATEADD(wk, DATEDIFF(wk, 0, @date), - 1)) as startdate,
               CONVERT(date, DATEADD(wk, DATEDIFF(wk, 0, @date), 5)) as enddate

    RETURN
于 2013-02-20T13:32:41.553 回答
1

我修改了 Valex 给出的代码以获得我需要的准确输出。

CREATE PROCEDURE dbo.SPReturnStartEndDateOfSpecifiedWeek
    @Week int,
    @P_startdate DateTime OUTPUT,
    @P_enddate DateTime OUTPUT
AS
    /* SET NOCOUNT ON */

    with C(i) as 
    ( 
      select CAST((DATEADD(mm, DATEDIFF(mm, 0, GETDATE()), 0)) as datetime) i
      UNION ALL
      select DATEADD (day,1,i) i from C 
      where DATEADD (day,1,i)<DATEADD(month,1,(DATEADD(mm, DATEDIFF(mm, 0, GETDATE()), 0)))
    ), 
    C1 as
    (
      select DATEPART(WEEK,i)-DATEPART(WEEK,(DATEADD(mm, DATEDIFF(mm, 0, GETDATE()), 0)))+1 WeekOfMonth,i from C
    ),
    C2 as
    (
        select WeekOfMonth,min(i) StartDate, max(i) EndDate from C1 group by WeekOfMonth
    )

    Select  @P_startdate = StartDate,
            @P_enddate = EndDate  
    from C2
    WHERE WeekOfMonth=@Week 

RETURN
于 2013-02-21T06:51:50.047 回答
0

你可以试试这个来获取一周的开始或结束日期。只需提供一个您可以找到日期的日期。

逻辑非常简单,只需获取一周的开始日期和结束日期,然后检查开始日期是否大于月份日期,如果是,则将月份日期作为周开始日期,否则将一周的开始日期。

摆弄一下以获得您想要的东西这是代码:

DECLARE @date datetime
SET @date = '2013-01-30'
DECLARE @startdate datetime
DECLARE  @enddate datetime
DECLARE @MonthStart datetime
Declare @MonthEnd datetime

SET @startdate = DATEADD(wk, DATEDIFF(wk, 6, @date), 6) 
SET @enddate =  DATEADD(dd,6, DATEADD(wk, DATEDIFF(wk, 6, @date), 6))
SET @monthStart = CONVERT(VARCHAR(25),DATEADD(dd,-(DAY(@date)-1),@date),101)
SET @MonthEnd =CONVERT(VARCHAR(25),DATEADD(dd,-(DAY(DATEADD(mm,1,@date))),DATEADD(mm,1,@date)),101)

--select @startdate wstart,@MonthStart monthstart,@enddate wend,@MonthEnd monthend
select  
[date] =  @date
,[week] = DATEPART(wk,@date)
,[WeekStartDate] = Case
                        WHEN @startdate <= @MonthStart then @MonthStart else @startdate  
                    END
,[WeekEndDate] = Case
                        WHEN @enddate > @MonthEnd then @MonthEnd else @enddate   
                    END

看这里:

小提琴

于 2013-02-20T10:42:25.397 回答