开发者

How do you calculate the number of weeks between two dates?

开发者 https://www.devze.com 2022-12-24 19:49 出处:网络
How do you calculate the number of weeks between two dates? 开发者_JAVA百科for example as follows

How do you calculate the number of weeks between two dates?

开发者_JAVA百科

for example as follows

Declare @StartDate as DateTime = "01 Jan 2009";
Declare @EndDate as DateTime = "01 June 2009";

@StartDate and @EndDate


Use the Datediff function. datediff(ww,@startdate,@enddate)

the ww tells the function what units you require the difference to be counted in.

http://msdn.microsoft.com/en-us/library/ms189794.aspx


You may use the following function to retrives week's between two dates:

CREATE FUNCTION [dbo].[fGetWeeksList]
(
    @StartDate DATETIME 
   ,@EndDate DATETIME 
)
RETURNS 
TABLE 
AS
RETURN
(

SELECT DATEADD(DAY,-(DATEPART(DW,DATEADD(WEEK, x.number, @StartDate))-2),DATEADD(WEEK, x.number, @StartDate)) as [StartDate]
      ,DATEADD(DAY,-(DATEPART(DW,DATEADD(WEEK, x.number + 1, @StartDate))-1) ,DATEADD(WEEK, x.number + 1, @StartDate)) AS [EndDate]
FROM master.dbo.spt_values x
WHERE x.type = 'P' AND x.number <= DATEDIFF(WEEK, @StartDate, DATEADD(WEEK,0,CAST(@EndDate AS DATE)))
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号