Bucle entre fechas en SQL Server

//Bucle entre fechas en SQL Server

Bucle entre fechas en SQL Server

El proceso espera dos parámetros de fecha, una fecha de inicio y una fecha final. A continuación, el proceso asignará una tercera variable, siete días después de la fecha de inicio. A continuación, el proceso ejecutará el código dado. El paso final dentro del bucle incrementa la fecha de inicio y la fecha de finalización por siete días. El proceso se bucle hasta que la fecha de inicio es mayor que la fecha final total.

declare @StartDate datetime
declare @StartDate      date
declare @EndDate        date
declare @OverallEndDate	date
declare @PrintStart     varchar(10)
declare @PrintEnd       varchar(10)

--	Define la fecha de inicio 
--	Define la fecha de finalización 
--	siete días después de la fecha de inicio
set @StartDate = '2018-01-01'
set @EndDate = dateadd( dd,7 ,@startDate)

--	Define la fecha final general
set @OverallEndDate = '2018-12-31'

--	Bucle mientras que la fecha de inicio es menor que la fecha final general
while(@OverallEndDate > @StartDate)
begin
        --     Imprima la semana en proceso
        set @PrintStart = CONVERT( varchar(10),@StartDate, 120)
        set @PrintEnd = CONVERT( varchar(10),@EndDate, 120)
        raiserror('Trabajando desde %s hasta %s' ,10, 1,@PrintStart ,@PrintEnd)

        --     El trabajo va aqui

        --     Increment the weeks
        set @StartDate = @EndDate
        set @EndDate = dateadd( dd,7 ,@startDate)

end
raiserror('Completo!' ,10, 1)

El ejemplo aparece debajo:

Sigue este projecto en GitHub:

By | 2017-12-28T11:25:37-06:00 December 27th, 2017|Categories: 100|Tags: , , , , , , |1 Comment

About the Author:

Mi nombre es Rodolfo y trabajo en Austin, Tejas. He estado trabajando con SQL Server desde 2003. Mi enfoque es SSRS, SSIS y el ajuste de rendimiento.

This Is A Custom Widget

This Sliding Bar can be switched on or off in theme options, and can take any widget you throw at it or even fill it with your custom HTML Code. Its perfect for grabbing the attention of your viewers. Choose between 1, 2, 3 or 4 columns, set the background color, widget divider color, activate transparency, a top border or fully disable it on desktop and mobile.

This Is A Custom Widget

This Sliding Bar can be switched on or off in theme options, and can take any widget you throw at it or even fill it with your custom HTML Code. Its perfect for grabbing the attention of your viewers. Choose between 1, 2, 3 or 4 columns, set the background color, widget divider color, activate transparency, a top border or fully disable it on desktop and mobile.