从该月的指定周获取startdate和enddate

时间:2013-02-20 09:48:36

标签: sql-server-2008 date

我需要从指定的(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 个答案:

答案 0 :(得分:3)

以下是查询给定月份的所有周开始日期和结束日期的查询。因此,您可以从此查询周中选择所需:

SQLFiddle example

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

答案 1 :(得分:1)

我写了一个程序来解决我的问题,这个程序将需要一周时间(1/2/3/4/5)作为输入,并将返回startdate和enddate

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

答案 2 :(得分: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

答案 3 :(得分: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

见这里:

Fiddle