Created
June 1, 2022 10:05
-
-
Save morriekken/9903ebb60d008d7019337cd62777dd2c to your computer and use it in GitHub Desktop.
Split dates into batches by batch size
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DECLARE @startDate DATE, @endDate DATE, @batchSize INT | |
SET @startDate = '2022-05-01' | |
SET @endDate = '2022-05-31' | |
SET @batchSize=4 | |
;WITH dates AS ( | |
SELECT @startDate as batchStartDate, DATEADD(DAY, @batchSize, @startDate) batchEndDate | |
UNION ALL | |
SELECT DATEADD(DAY, 1, batchEndDate) batchStartDate, DATEADD(DAY, 1 + @batchSize, batchEndDate) batchEndDate | |
FROM dates | |
WHERE batchEndDate<@endDate | |
) | |
SELECT | |
batchStartDate, | |
CASE WHEN batchEndDate>@endDate THEN @endDate ELSE batchEndDate END batchEndDate | |
FROM dates |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment