2010-03-29 19 views
6

¿Cómo se calcula el número de semanas entre dos fechas?¿Cómo se calcula el número de semanas entre dos fechas?

por ejemplo como sigue

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

@StartDate and @EndDate 
+0

Do desea saber cuántos períodos de 7 días entre las dos fechas, o cuántos domingos (o lunes) se encuentran en el período? – Gabe

Respuesta

1

puede usar la siguiente función para retrives semana de entre dos fechas:.

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))) 
Cuestiones relacionadas