sql开始一周结束

时间:2016-05-16 16:06:27

标签: sql sql-server-2005

我无法获得Sql Server中两个日期之间的开始和结束周?例如:

    begin       end
    2016-04-25  2016-05-01
    2016-05-02  2016-05-08
    2016-05-09  2016-05-15
    2016-05-16  2016-05-22
    2016-05-23  2016-05-29
    2016-05-30  2016-06-05
...

问候!

4 个答案:

答案 0 :(得分:0)

SQL为此目的具有DATEDIFF(datepart,startdate,enddate)函数。

示例:

SELECT DATEDIFF(day,'2014-06-05','2014-08-05') AS DiffDate

结果

61

详细了解DATEFIFF here

答案 1 :(得分:0)

在SQL Server中使用DATEDIFF()函数

答案 2 :(得分:0)

您需要一个日期表来执行此操作

Declare @start_date datetime = '2016-04-25'
        @end_date datetime = '2016-06-05'

SELECT Min(dt) AS week_start, 
       Max(dt) AS week_end 
FROM   dates_table 
WHERE  date_col > @start_date 
       AND date_col < @end_date 
GROUP  BY Datepart(week, dt), 
          Year(dt) 
ORDER  BY Datepart(week, dt), 
          Year(dt) 

要创建日期表并填充日期,以下是关于在日期范围之间生成日期的几个问题

  1. How to generate a range of dates in SQL Server

  2. Generate Dates between date ranges

答案 3 :(得分:0)

试试这个:

multithreaded_debugger